1

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?

4
  • 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(). Commented Mar 7, 2011 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. Commented Mar 8, 2011 at 0:03
  • @CRD: NSArray is immutable. You cannot add an item in-place. Commented Mar 8, 2011 at 0:11
  • 1
    @Seva: Yes I meant NSMutableArray of course, question asked about adding items to the array. Commented Mar 8, 2011 at 4:57

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.