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.   

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章