一.类的继承结构探索
- 以下面列子lldb 输出来探索
SBPerson *p = [SBPerson alloc];
(lldb) p/x p
(SBPerson *) $0 = 0x0000000100741940
(lldb) x/4gx 0x0000000100741940
0x100741940: 0x001d8001000083a9 0x0000000000000000
0x100741950: 0x50626154534e5b2d 0x65695672656b6369
- 从分析isa的结构体可以得出x86的掩码是0x00007ffffffffff8和isa与上可以得到class
(lldb) p/x 0x001d8001000083a9 &0x00007ffffffffff8
(long) $1 = 0x00000001000083a8
(lldb) po 0x00000001000083a8
SBPerson
- 从上面输出可以得出isa 最终指向类,带上疑问去探索一下0x00000001000083a8这个类的内存结构
(lldb) x/4gx 0x00000001000083a8
0x1000083a8: 0x0000000100008380 0x00007fff8da57118
0x1000083b8: 0x00007fff66307140 0x0000801000000000
(lldb) p/x 0x0000000100008380 &0x00007ffffffffff8
(long) $3 = 0x0000000100008380
(lldb) po 0x0000000100008380
SBPerson
思考: 0x0000000100008380 和 0x00000001000083a8 为什么都指向同一个类呢?
1.猜想 类会和我们的对象 无限开辟 内存不止有一个类
-
2.这个类是否真实的存在?
从上面类的输出信息中并没有找到0x0000000100008380 ????
-
用MachOView在验证一下:
1.在symbolTable 搜索class找到_OBJC_METACLASS 从以上验证可以得出“元类”是由编译器自动生成并不在类中
-
2.由此可以得出类的走向图
-
类的继承链走向
-
从输出可以得出类的继承走向
NSObject 元类链
void lgTestNSObject(void){
// NSObject实例对象
NSObject *object1 = [NSObject alloc];
// NSObject类
Class class = object_getClass(object1);
// NSObject元类
Class metaClass = object_getClass(class);
// NSObject根元类
Class rootMetaClass = object_getClass(metaClass);
// NSObject根根元类
Class rootRootMetaClass = object_getClass(rootMetaClass);
NSLog(@"\n%p 实例对象\n%p 类\n%p 元类\n%p 根元类\n%p 根根元类",object1,class,metaClass,rootMetaClass,rootRootMetaClass);
// LGPerson元类
Class pMetaClass = object_getClass(LGPerson.class);
Class psuperClass = class_getSuperclass(pMetaClass);
NSLog(@"%@ - %p",psuperClass,psuperClass);
// LGTeacher -> LGPerson -> NSObject
// 元类也有一条继承链
Class tMetaClass = object_getClass(LGTeacher.class);
Class tsuperClass = class_getSuperclass(tMetaClass);
NSLog(@"%@ - %p",tsuperClass,tsuperClass);
// NSObject 根类特殊情况
Class nsuperClass = class_getSuperclass(NSObject.class);
NSLog(@"%@ - %p",nsuperClass,nsuperClass);
// 根元类 -> NSObject
Class rnsuperClass = class_getSuperclass(metaClass);
NSLog(@"%@ - %p",rnsuperClass,rnsuperClass);
}
- 输出如下:
0x102005c60 实例对象
0x7fff8da57118 类
0x7fff8da570f0 元类
0x7fff8da570f0 根元类
0x7fff8da570f0 根根元类
2021-06-18 11:57:47.214443+0800 002-isa分析[2126:73900] SBPerson元类NSObject - 0x7fff8da570f0
2021-06-18 11:57:47.214529+0800 002-isa分析[2126:73900] 元类继承链:SBPerson - 0x100008378
2021-06-18 11:57:47.214588+0800 002-isa分析[2126:73900] NSObject 根类特殊情况:(null) - 0x0
2021-06-18 11:57:47.214647+0800 002-isa分析[2126:73900] 根元类 -> NSObject:NSObject - 0x7fff8da57118
-
从上面输出结果得出走向图
-
从上面的走向和继承链可以得出整个isa的流程图
二.内存偏移
- 例一
// 数组指针
long c[4] = {1,2,3,4};
long *d = c;
NSLog(@"%p - %p - %p",&c,&c[0],&c[1]);
NSLog(@"%p - %p - %p",d,d+1,d+2);
for (long i = 0; i<4; i++) {
long value = *(d+i);
NSLog(@"%ld",value);
}
- 输出如下
2021-06-18 14:08:01.746706+0800 002-内存偏移[2518:101007] 0x7ffeefbff480 - 0x7ffeefbff480 - 0x7ffeefbff488
2021-06-18 14:08:01.748988+0800 002-内存偏移[2518:101007] 0x7ffeefbff480 - 0x7ffeefbff488 - 0x7ffeefbff490
2021-06-18 14:08:01.749146+0800 002-内存偏移[2518:101007] 1
2021-06-18 14:08:01.749234+0800 002-内存偏移[2518:101007] 2
2021-06-18 14:08:01.749306+0800 002-内存偏移[2518:101007] 3
2021-06-18 14:08:01.749401+0800 002-内存偏移[2518:101007] 4
- 例二
// 数组指针
int c[4] = {1,2,3,4};
int *d = c;
NSLog(@"%p - %p - %p",&c,&c[0],&c[1]);
NSLog(@"%p - %p - %p",d,d+1,d+2);
for (int i = 0; i<4; i++) {
int value = *(d+i);
NSLog(@"%d",value);
}
2021-06-18 14:15:51.513568+0800 002-内存偏移[2578:104605] 0x7ffeefbff490 - 0x7ffeefbff490 - 0x7ffeefbff494
2021-06-18 14:15:51.514767+0800 002-内存偏移[2578:104605] 0x7ffeefbff490 - 0x7ffeefbff494 - 0x7ffeefbff498
2021-06-18 14:15:51.514864+0800 002-内存偏移[2578:104605] 1
2021-06-18 14:15:51.514934+0800 002-内存偏移[2578:104605] 2
2021-06-18 14:15:51.515003+0800 002-内存偏移[2578:104605] 3
2021-06-18 14:15:51.515047+0800 002-内存偏移[2578:104605] 4
从例一和例二的输出结果总结出: 类的地址值 = 首地址 + 偏移值(步长int 类型步长是4 long 类型 是8)
例3看下对象
LGPerson *p1 = [LGPerson alloc];
LGPerson *p2 = [LGPerson alloc];
LGPerson *p3 = [LGPerson alloc];
LGNSLog(@"%@ -- %p",p1,&p1);
LGNSLog(@"%@ -- %p",p2,&p2);
LGNSLog(@"%@ -- %p",p3,&p3);
NSArray *array = @[p1,p2,p3];
LGPerson *t = array[0];
LGPerson *t1 = array[1];
LGPerson *t2 = array[2];
LGNSLog(@"%p - %p - %p",&t,&t1,&t2);
- 输出结果如下
KC打印: <LGPerson: 0x100468880> -- 0x7ffeefbff478
KC打印: <LGPerson: 0x100468030> -- 0x7ffeefbff470
KC打印: <LGPerson: 0x100466ba0> -- 0x7ffeefbff468
KC打印: 0x7ffeefbff458 - 0x7ffeefbff450 - 0x7ffeefbff448
- 总结:对象的内存地址排列是根据首地址依次排列 + 偏移值(偏移值跟对象的类型有关,偏移值就是 所谓的“步长”),排列的循序是有高地址指向地地址