OC

内存管理-内存布局

Posted on 2022-01-06,1 min read

简述内存布局的基本规则

示意图

Tz9Cvj.png

int a = 10;
int b;

- (void)test{
    static int c = 20;
    static int d;
    
    int e;
    int f = 20;
    
    NSString *str = @"test";
    NSObject *obj = [[NSObject alloc]init];
    
    NSLog(@"\n&a = %p\n&b = %p\n&c = %p\n&d = %p\n&e = %p\n&f = %p\nstr = %p\nobj = %p\n",&a,&b,&c,&d,&e,&f,str,obj);
}
// 内存地址从小到大排序
/**
 字符串常量
 str = 0x10dd440f8
 
 已初始化的全局变量、静态变量
 &a = 0x10dd49d68
 &c = 0x10dd49d6c
 
 未初始化的全局变量、静态变量
 &b = 0x10dd49f10
 &d = 0x10dd49ef0
 
 堆
 obj = 0x6000029f4230
 
 栈 ↑
 &f = 0x7ff7b21bd658
 &e = 0x7ff7b21bd65c
*/

下一篇: 内存管理-GCD定时器→

loading...