Take the 2-minute tour ×

When an array is created in Objective-C using alloc init, how is the memory managed when objects are added to the array?

I would like to know how this relates to C when you create an array and malloc, the allocated size needs to be the size of the expected array or the array memory needs to be reallocated.

How do these relate or what is a good way to understand the way the C code works.

Does the objective-c arrays memory get handled internally when objects are added or how does this work?

share|improve this question
How exactly is memory handled inside NSArray is an implementation detail - that is, no single right answer. It can vary between compilers, versions, runtime libraries, OSes, etc. That said, most likely via malloc(). –  Seva Alekseyev Mar 7 '11 at 23:32
About the only thing you can say with near certainty is that an NSArray will use more memory than a C array (you pay for the features) but that the delta cost of adding an item is probably cheaper for the NSArray as the whole array will not be reallocated, which is probable (but not certain) for the C array. –  CRD Mar 8 '11 at 0:03
@CRD: NSArray is immutable. You cannot add an item in-place. –  Seva Alekseyev Mar 8 '11 at 0:11
@Seva: Yes I meant NSMutableArray of course, question asked about adding items to the array. –  CRD Mar 8 '11 at 4:57
add comment

2 Answers

up vote 2 down vote accepted

An Objective-C array follows the same memory management rules as other Objective-C objects. If you allocate it, you'll need to release it.

C arrays and Objective-C arrays are similar in concept, but implemented quite differently. A C array is a contiguous block of memory with very little other than a language construct governing how you use it. Objective-C arrays are objects with significant built-in functionality. They dynamically resize themselves (if they're mutable) to accomodate added elements. They properly retain and release the objects that they store. They can sort themselves, filter themselves, insert objects, delete objects, etc. You should make no assumptions about how they're implemented.

Apple's documentation should give you a much better idea of what's possible with Objective-C arrays (and while you're at it, look at the other collection classes too). Start reading here: Collections Programming Topics

share|improve this answer
add comment

NSArrays (And by extension NSMutableArrays) retain the objects added to them, and send them all release messages when the array itself is deallocated.

The upshot of this is that a common pattern is to alloc an object, initialize it, hand it to the array, and then release it. Since the array retains it for itself, it'll last as long as the array itself does, or until you instruct the array to get rid of it. You will never need to send it another release message, the array machinery takes care of that.

I'd guess that the Mutable arrays are implemented as something like a linked list, so they can be easily expanded and contracted later. It's just a list of pointers, and maybe a little metadata. Since it doesn't create any objects handed to it, just puts it's own leash on them, all it needs to do it have a place for the pointer to the object.

share|improve this answer
add comment

Your Answer


