Suppose I have a custom class:
假设我有一个自定义类:
@interface someClass : NSObject
@property (nonatomic, assign) int intProperty;
@end
and it has a method:
它有一个方法:
- (void)test {
NSLog(@"%p", &(_intProperty));
}
Execute the following code:
执行以下代码:
SomeClass* someClass = [[SomeClass alloc] init];
someClass.intProperty = 3;
[someClass test];
The output is:
输出是:
2016-12-05 19:42:33.001951 test[2316:881536] 0x17401d078
Looks like an address on the heap. But as we know, you can't store a primitive type variable on the heap. So how does it read an int from a pointer to a heap address?
看起来像堆上的地址。但正如我们所知,您无法在堆上存储基本类型变量。那么它如何从指向堆地址的指针读取int?
Providing relevant links will be appreciated. Thanks in advance:)
提供相关链接将不胜感激。提前致谢:)
1 个解决方案
#1
1
When you call SomeClass alloc
method, it allocates memory on the heap, the memory stores variables of SomeClass
instance including intProperty
.
当你调用SomeClass alloc方法时,它会在堆上分配内存,内存存储SomeClass实例的变量,包括intProperty。
We suppose you didn't use @synthesize to rename the instance variable of intProperty
, so the backup variable of intProperty
property is _intProperty
.
我们假设您没有使用@synthesize重命名intProperty的实例变量,因此intProperty属性的备份变量是_intProperty。
someClass.intProperty = 3;
this line of code stores 3 to the address of _intProperty
. The compiler can figure out the address of _intProperty
which is the address of someClass
plus the a offset, the offset is the distance of the _intProperty
variable address from the begin of someClass
. As we know the address of _intProperty
, we can read from it or write to it.
someClass.intProperty = 3;这行代码将3存储到_intProperty的地址。编译器可以找出_intProperty的地址,它是someClass的地址加上一个偏移量,偏移量是_intProperty变量地址与someClass开头的距离。我们知道_intProperty的地址,我们可以从中读取或写入它。
But as we know, you can't store a primitive type variable on the heap
但正如我们所知,您无法在堆上存储基本类型变量
It's wrong, we can store a primitive on heap, the code below store a integer value on the heap.
这是错误的,我们可以在堆上存储一个原语,下面的代码在堆上存储一个整数值。
int *a = (int *)malloc(sizeof(int)); // a is a pointer, its value is the address on the heap
*a = 4; // store the value 4 to the address on the heap
free(a); // you must free the memory.
#1
1
When you call SomeClass alloc
method, it allocates memory on the heap, the memory stores variables of SomeClass
instance including intProperty
.
当你调用SomeClass alloc方法时,它会在堆上分配内存,内存存储SomeClass实例的变量,包括intProperty。
We suppose you didn't use @synthesize to rename the instance variable of intProperty
, so the backup variable of intProperty
property is _intProperty
.
我们假设您没有使用@synthesize重命名intProperty的实例变量,因此intProperty属性的备份变量是_intProperty。
someClass.intProperty = 3;
this line of code stores 3 to the address of _intProperty
. The compiler can figure out the address of _intProperty
which is the address of someClass
plus the a offset, the offset is the distance of the _intProperty
variable address from the begin of someClass
. As we know the address of _intProperty
, we can read from it or write to it.
someClass.intProperty = 3;这行代码将3存储到_intProperty的地址。编译器可以找出_intProperty的地址,它是someClass的地址加上一个偏移量,偏移量是_intProperty变量地址与someClass开头的距离。我们知道_intProperty的地址,我们可以从中读取或写入它。
But as we know, you can't store a primitive type variable on the heap
但正如我们所知,您无法在堆上存储基本类型变量
It's wrong, we can store a primitive on heap, the code below store a integer value on the heap.
这是错误的,我们可以在堆上存储一个原语,下面的代码在堆上存储一个整数值。
int *a = (int *)malloc(sizeof(int)); // a is a pointer, its value is the address on the heap
*a = 4; // store the value 4 to the address on the heap
free(a); // you must free the memory.