- 系统分配给了16个字节给NSOject 对象, (通过malloc_size获得)
2.但是NSObject 对象内部使用8个字节的空间 (64位环境下, 可以通过class_getInstanceSize函数获得)


clang -rewrite-objc main.m -o main.cpp 将OC 改成C++文件 , 不是很建议使用这行代码, 因为不同的平台, 是 不一样的
尤其是汇编语言, 依托于硬件, 所有我们可以 指定平台
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc OC源文件 -o 输出的 cpp文件
xcrun : Xcode Run
struct NSObject_IMPL {
Class isa;
};



#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <malloc/malloc.h>
//@interface Student : NSObject{
// int _age;
// double _height;
// NSString *_name;
//
//}
//
//
//
//
//@end
//
//@implementation Student
//
//
//
//@end
//C++
//struct NSObject_IMPL {
// Class isa; //8个字节
//};
//
//
main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSObject *obj = [[NSObject alloc] init];
//16 个字节 , 真正利用起来的只有8个字节
/**
struct Student{
int age;
double height;
}
*/
//8 个字节 获取类的实例对象的大小 , 并不是真正分配的内存大小, 返回的是类的实例对象的成员变量所占的内存大小
NSLog(@"%ld",class_getInstanceSize([NSObject class]));
//16个字节, 返回指针所指向那边内存的大小 获得obj所指向的内存的大小
NSLog(@"%ld", malloc_size((__bridge const void *)(obj)));
}
return 0;
``}
// CF requires all objects be at least 16 bytes. Foundation 框架下的源码
if (size < 16) size = 16;






一个Student对象, 占用多少内存??




iOS是小端模式, 高地址在后面





方法不放在实例对象里面. 因为alloc init 会创建不同的实例对象, 而方法是不变的, 方法存一份就好了
网友评论