NSArray的大小 [英] Size of NSArray
问题描述
当我试图检查声明没有任何容量的NSArray的大小时,我发现它4.现在问题是为什么它总是4?请帮我找到它....
谢谢
When I tried to check the size of NSArray which is declared without any capacity, I found it 4. Now the question is why it is always 4? please help me out to find it.... Thanks
推荐答案
鉴于此:
NSArray *foo;
NSLog(@"%d", sizeof(foo));
您将获得4或8,具体取决于您使用的是32位还是64位系统。请注意,我非常有目的地没有初始化 foo
;没有必要这样做 sizeof(foo)
给 foo
和 foo
只是指向对象的随机指针。如果那是 id foo就没关系;无效* foo的; NSString * foo;
all都是4或8.
You'll either get 4 or 8, depending on if you are on a 32 or 64 bit system. Note that I quite purposefully didn't initialize foo
; there is no need to do so as sizeof(foo)
is giving the bytesize of foo
and foo
is just a random pointer to an object. Wouldn't matter if that were id foo; void*foo; NSString*foo;
all would be 4 or 8.
如果你想要特定类的实例的分配大小,那么Objective- C运行时提供了可以完全实现的内省API。但是,我真的没有想到为什么这会比程序中的有趣更多。
If you want the allocated size of an instance of a particular class, the Objective-C runtime provides introspection API that can do exactly that. However, I can't really think of any reason why that would be more than passingly interesting in a program.
请注意,实例的分配大小不会占用任何子分配。即NSArray可能有一个支持商店,这是一个单独的分配。
Note that the allocation size of an instance does not account for any sub-allocations. I.e. an NSArray likely has a backing store which is a separate allocation.
重申:
这篇关于NSArray的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!