简体   繁体   English

Objective-C:存储的对象的实例变量对应的指针在哪里

[英]Objective-C: Where is the pointer that corresponds to an instance variable of an object stored

Suppose I have the following class definition假设我有以下类定义

@interface ClassX: NSObject
@property NSArray *arr;
@end

Suppose I have the following lines in a method假设我在一个方法中有以下几行

-(void)someMethod
{
  ClassX *obj = [ClassX new];
  obj.arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil]; //Edit to avoid getting derailed by differences due to @ notation
}

What is in the stack, what is in the heap?栈里有什么,堆里有什么?

Stack:堆:

obj in the stack. obj 在堆栈中。 Value of this variable is the address of the place in the heap where obj resides.该变量的值是obj所在的堆中的地址。

Heap:堆:

Object of type ClassX ClassX 类型的对象

What about the NSArray and the NSNUmbers within the NSArray? NSArray 和 NSArray 中的 NSNUMbers 怎么样? Are they also divided as above?他们也像上面那样划分吗? Ie The pointer to the NSArray is in the stack and the NSArray object is in the heap?即指向 NSArray 的指针在堆栈中,而 NSArray 对象在堆中? If yes, what is contained in the heap memory that contains the "obj" object?如果是,那么包含“obj”对象的堆内存中包含什么?

The figures I have seen are that this block of memory in the Heap contains a isa pointer and then instance variables.我看到的数字是,Heap中的这块内存包含一个isa指针,然后是实例变量。 The isa pointer point to the location of the Class structure . isa指针指向Class structure的位置。 Ie this piece of memory contains another isa pointer followed by Method structs .即这块内存包含另一个 isa 指针,后跟Method structs Each struct has a selector and a pointer to where the corresponding implementation starts.每个结构都有一个selector和一个pointer相应实现开始位置的pointer

This seems to suggest that the area variable within the object is in the heap.这似乎表明对象内的区域变量在堆中。 Ie the pointer to the NSArray object is in the heap.即指向 NSArray 对象的指针在堆中。

EDIT Based on answers below, is this a correct view of what is there in heap and stack?编辑根据下面的答案,这是堆和堆栈中的内容的正确视图吗?

The stack will contain 4 pointers, One to obj, one to NSArray, two to NSNumber objects.堆栈将包含 4 个指针,一个指向 obj,一个指向 NSArray,两个指向 NSNumber 对象。

The heap memory that starts at the address pointed to by obj has 1. the isapointer pointing ClassX class structure 2. Followed by memory to store NSArray object.obj指向的地址开始的堆内存有1.指向ClassX类结构的isapointer 2. isapointer跟着内存来存放NSArray对象。

This NSArray object memory has 1. isapointer pointing to NSArray class structure 2. followed by memory to store 2 NSNumber objects.这个 NSArray 对象内存有 1. isapointer 指向 NSArray 类结构 2. 后跟内存来存储 2 个 NSNumber 对象。

The NSNUmber object memory contain 1. A isapointer pointing to NSNumber class structure 2. Followed by memory to store 2 ints NSNUMer 对象内存包含 1. 一个指向 NSNumber 类结构的 isapointer 2. 后跟内存来存储 2 个整数

Both NSArray and NSNumber objects are heap-allocated, not stack-allocated. NSArrayNSNumber对象都是堆分配的,而不是堆栈分配的。

They do not look like heap objects because the compiler hides it from you using the relatively new syntax @[] .它们看起来不像堆对象,因为编译器使用相对较新的语法@[]将它隐藏起来。

When you write当你写

@[@1, @2]

The compiler translates it to编译器将其翻译为

[NSArray arrayWithObjects: {[NSNumber numberWithInt:1], [NSNumber numberWithInt:2]} count: 2]

The end result is as follows: the stack has a pointer to an object of type ClassX .最终结果如下:堆栈有一个指向ClassX类型对象的指针。 The memory pointed to by this pointer starts with an isapointer, followed by a pointer to another location in the heap that has NSArrray object in it.此指针指向的内存以 isapointer 开头,后跟指向堆中另一个位置的指针,该位置中包含NSArrray对象。 In turn, this part of heap memory has another isapointer followed by a pointer to a block of memory representing the inner array of NSArray , containing two pointers to NSNumber objects.反过来,这部分堆内存有另一个 isapointer,后跟一个指向表示NSArray内部数组的内存块的指针,其中包含两个指向NSNumber对象的指针。 Each of the NSNumber objects has an isapointer followed by data which represents the number.每个NSNumber对象都有一个 isapointer 后跟表示数字的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM