检查对象的在Objective-C尺寸 [英] Checking the size of an object in Objective-C

查看:89
本文介绍了检查对象的在Objective-C尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个Objective-C对象的大小。我使用了类似于:

I'm trying to find the size of an objective-c object. I'm using something similar to:

    NSLog(@"sizeof myObject: %ld", sizeof(*myObject));

这只是给我的指针的大小虽然。

That just gives me the size of the pointer though.

我在做什么错了?

推荐答案

全部编译器知道的是指针,这就是为什么你得到指针的大小了。要查看分配对象的大小,请使用以下code片段之一:

All the compiler knows about is the pointer, which is why you're getting the size of the pointer back. To see the size of the allocated object, use one of the following code snippets:

#import <malloc/malloc.h>

// ...

NSLog(@"Size of %@: %zd", NSStringFromClass([myObject class]), malloc_size((__bridge const void *) myObject));

无ARC:

#import <malloc/malloc.h>

// ...

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

迈克·阿什拥有不俗的写了一些对他的Q&安培的对象 - C运行时内部的;一个博客:<一href=\"http://mikeash.com/?page=pyblog/friday-qa-2009-03-13-intro-to-the-objective-c-runtime.html\">http://mikeash.com/?page=pyblog/friday-qa-2009-03-13-intro-to-the-objective-c-runtime.html

这篇关于检查对象的在Objective-C尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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