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
When I tried to check the size of NSArray which is declared without any
Share
Given this:
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 assizeof(foo)is giving the bytesize offooandfoois just a random pointer to an object. Wouldn’t matter if that wereid foo; void*foo; NSString*foo;all would be 4 or 8.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.
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.
To reiterate:
sizeof(foo) in the above code has nothing to do with the size of the allocated instance.