OC对象窥探2

142 阅读1分钟

上次我们讲到最基本的对象这次我们换一个一下。

@interface Student : NSObject{
  @public
  int _no;
  int _age;
}
@end

@implementation Student

@end

定义一个对象Student *student = [[Student alloc]init]; 同样的生成.cpp文件 可以看到

struct Student_IMPL {
	struct NSObject_IMPL NSObject_IVARS;
	int _no;
	int _age;
};
这个其实可以翻译成
struct Student_IMPL {
	Class isa;
	int _no;
	int _age;
};

同样的我们打印大小

NSLog(@"%zd",malloc_size((__bridge const void *)(student)));

NSLog(@"%zd",class_getInstanceSize([Student class]));

image.png 我们在打印下内存地址:

WeChat470ce3764bfc486a713ad6bf1418afd3.png

这下可以看到地址是16位 为什么地址会有 04,05 这个其实是与tagpoint有关系的 我们也可以修改地址然后你会发现修改了地址 no,age的值也发生了改变