NSArray Size

When I tried to check the size of the NSArray that was declared without any capacity, I found it 4. Now the question is, why is it always 4? please help me find it .... Thanks

+8
objective-c ios4
source share
2 answers

Considering this:

NSArray *foo; NSLog(@"%d", sizeof(foo)); 

You will get either 4 or 8, depending on whether you are using a 32 or 64-bit system. Note that I intentionally did not initialize foo ; there is no need to do this, since sizeof(foo) gives the byte foo , and foo is just a random pointer to an object. It would not matter if it was id foo; void*foo; NSString*foo; id foo; void*foo; NSString*foo; everything would be 4 or 8.

If you want the allocated size of an instance of a particular class, the Objective-C runtime provides an introspection API that can do just that. However, I cannot think of any reason why this would be more than curious in the program.

Note that the instance allocation size does not account for any sub-distributions. That is, NSArray probably has backup storage, which is a separate distribution.

Repeat:

sizeof (foo) in the above code has nothing to do with the size of the selected instance.

+6
source share

If you are talking about sizeof , this is the wrong way to find out how much data is stored by NSArray. Objective-C objects are always accessible through pointers, and the size of the pointer on the iPhone is 4 bytes. This is what sizeof tells you. To find out how many objects are in the array, ask the array about its count .

+13
source share

All Articles