iOS 开发 Objective-C

iOS 底层 day24 内存管理 内存布局 Tagged Po

2020-09-22  本文已影响0人  望穿秋水小作坊

一、iOS 程序内存布局

1.重点掌握下图,我们可以理解很多东西
内存布局图
2. 通过下面代码,强化我们的理解
void func(){    //代码区
    printf("func");
}
const int a = 10;   //常量区
int c = 20; //全局数据区
int main() {
    NSString *b = @"我是chinese"; // 字符串在常量区; &b在栈区
    static int d = 30;  //全局数据区
    NSObject *e = [[NSObject alloc] init];//对象在堆区; &e在栈区
    NSLog(@"%p",func);
    NSLog(@"\na=%p\nb=%p\nc=%p\nd=%p\ne=%p\n&e=%p\n", &a, b, &c, &d, e, &e);
}

二、Tagged Pointer

1. Tagged Pointer基本介绍
2. 如何判断一个指针是否为 Tagged Pointer
3. Tagged Pointer 从代码中窥探它们
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSNumber *a = @1;
    NSNumber *b = @2;
    NSNumber *c = @(2020202020202002022);
    NSLog(@"%p,%p,%p",a,b,c);
    
    NSString *aa = [NSString stringWithFormat:@"123abc"];
    NSString *aa2 = [NSString stringWithFormat:@"123abc"]; // 发现 aa2 的地址值和 aa 是相同的
    NSString *bb = [NSString stringWithFormat:@"abcd"];
    NSString *cc = [NSString stringWithFormat:@"abcabcabcabcabcabcabcabc"];
    NSLog(@"%p,%p,%p,%p",aa,aa2,bb,cc);
    NSLog(@"%@,%@,%@", [aa class], [bb class], [cc class]);
}
Demo[5902:460945] 0xb8736ae365a09e56,0xb8736ae365a09e66,0x600000b6cfa0
Demo[5902:460945] __NSCFNumber,__NSCFNumber,__NSCFNumber
Demo[5902:460945] 0xa8755cc57693bd52,0xa8755cc57693bd52,0xa8736ae52396b850,0x60000059ef40
Demo[5902:460945] NSTaggedPointerString,NSTaggedPointerString,__NSCFString
上一篇下一篇

猜你喜欢

热点阅读