malloc_size和sizeof有什么不同 [英] what is the different with malloc_size and sizeof

查看:843
本文介绍了malloc_size和sizeof有什么不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ojb-c,我想知道一个对象的内存大小,但是我知道了,

I am using Ojb-c, and I want to know the memory size of an object, but I got this,

NSObject *obj = [[[NSObject alloc] init] autorelease];

NSLog(@"malloc size of myObject: %zd", malloc_size(obj));
NSLog(@"size of myObject: %zd", sizeof(obj));

  malloc size of myObject: 16
  size of myObject: 4

我知道sizeof(obj)是4,因为ios 32上的指针大小是4个字节,有什么区别?

I know the sizeof(obj) is 4, because the pointer size on ios 32 is 4 bytes, what is the difference?

但除此之外,

@interface TestObj : NSObject

@property (nonatomic, retain) NSArray *arr;
@property (nonatomic, assign) int count;

@end

@implementation TestObj

@end


    TestObj *obj2 = [[[TestObj alloc] init] autorelease];

    NSLog(@"malloc size of obj2: %zd", malloc_size(obj2));
    NSLog(@"size of obj2: %zd", sizeof(obj2));

  malloc size of obj2: 16
  size of obj2: 4

我怎么知道TestObj的实际大小?谢谢.

how could I know the real size of TestObj ? thanks.

推荐答案

sizeof对象是将对象存储在内存中所需的空间. malloc_size是为其实际分配了多少空间(例如,在具有固定大小的池的内存分配系统中,根据使用的其他内存量,可能会为您分配不同的空间).

The sizeof the object is how much space it requires for that object to be stored in memory. The malloc_size is how much space was actually allocated for it (for example, in a memory allocation system with fixed-size pools, you might be allocated a different amount depending on how much other memory was in use).

这篇关于malloc_size和sizeof有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆