Objective-C 中的堆栈和堆地址

Stack and Heap addresses in Objective-C

我在 Objective-C 中做了很多关于内存分配的研究,并且我已经阅读了很多关于这方面的文章和博客,但我仍然有一些不清楚的时刻。我知道对象类型存储在堆中,原始类型存储在堆栈中,但是您能否向我解释一下所列示例的更多信息:

NSObject *obj;

NSLog(@"%p", obj); //prints 0x0 which means address in stack ?
NSLog(@"%p", &obj); //prints 0x7ffee427bf68 which means address in heap ?

obj = [[NSObject alloc] init];

NSLog(@"%p", obj); //prints 0x6000000119b0 which means address in stack ?
NSLog(@"%p", &obj); //prints 0x7ffee427bf68 which means address in heap ?

基本类型相同:

int value = 23;

NSLog(@"%p", value); //prints 0x17 - is that a stack address ?
NSLog(@"%p", &value); //prints 0x7ffeea19bf6c - is that a stack address too ?

块本地的任何变量都将存储在堆栈内存中,即使它是指针,但如果您使用指针来存储地址并且该地址来自动态内存,那么它将存储在堆内存中,例如:

Func()
{
int *a ;    
int b;   --stack memory
a = malloc (sizeof(int));    
Printf &a;-----this will be stack memory pointer and will be store in stack memory.   
Print a;-----this will point to address which is allocate by heap this will be store in heap memory.   

}