Objective - C 内存管理(三)内存布局
2020-04-13 本文已影响0人
爱玩游戏的iOS菜鸟
(一)iOS程序的内存布局
内存布局下面如何验证:
//我们在main函数中,直接添加下面的变量 并将其地址输出
int a = 10;
int b;
int main(int argc, char * argv[]) {
NSString * appDelegateClassName;
@autoreleasepool {
static int c = 20;
static int d;
int e;
int f = 20;
NSString *str = @"123";
NSObject *obj = [[NSObject alloc]init];
NSObject *obj2 = [[NSObject alloc]init];
NSLog(@"\n全局已初始化\t\ta = %p\n全局未初始化\t\tb = %p\n静态已初始化变量\tc = %p\n静态未初始化变量\td = %p\n变量未初始化\t\te = %p\n变量已初始化\t\tf = %p\n字符串常量\t str = %p\n类对象\t\t obj = %p\n类对象2\t\t obj = %p\n",&a,&b,&c,&d,&e,&f,str,obj,obj2);//str与obj存放的就是实际地址的指向 自身还是在栈区
// Setup code that might create autoreleased objects goes here.
appDelegateClassName = NSStringFromClass([AppDelegate class]);
}
return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}
输出结果,进行比对:
//数据段
//字符串常量区
str = 0x108eca198
//已初始化数据区
&a = 0x108ecc838
&c = 0x108ecc83c
//未初始化数据区
&d = 0x108ecc918
&b = 0x108ecc91c
//堆 从小到大
obj = 0x600000d68040
obj2 = 0x600000d680a0
//栈 从大到小
&e = 0x7ffee6d37d04
&f = 0x7ffee6d37d00
即可证明。之后我们通过逆向也可证明,暂不讲述。