3 Replies Latest reply: Oct 22, 2010 2:47 AM by RayNewbie
m_userName Level 1 Level 1 (0 points)
I want to declare an array of in objective C. The size of the array is not known until runtime, but once it is allocated, the array will not need to be resized.

In C++, it would be something similar to:

int* myArray;
myArray = new int[x]; // x is a variable which in not known at compile time.

I've had a look at the apple reference library, and noticed that NSArray does not support any InitWithCapacity method. NSMutable array does seem to support this method, however in the documentation it said that mutable arrays expand as needed (similar to STL vectors), which might be a bit much for my requirements (especially in the case of using integers, since I'd have to cast them back an forth to NSInteger objects).

I just wondered what the recommended way to allocate fixed-size whose size is not known until runtime is with objective C (whether you should use C-style malloc, NSMutableArrays, etc.).

MacBook Pro, iOS 4
  • RayNewbie Level 5 Level 5 (6,810 points)
    m_userName wrote:
    I want to declare an array of in objective C. The size of the array is not known until runtime, but once it is allocated, the array will not need to be resized.

    That would be NSArray.
    I've had a look at the apple reference library, and noticed that NSArray does not support any InitWithCapacity method.

    NSArray is immutable, which means its contents (and therefore its size) can only be set once, during init. There's also no such thing as empty space in NSArray. You may not, for example, init an instance with one or more nil pointers (though there's a NSNull class for use when placeholders are needed, but you could only replace such a placeholder in an instance of NSMutable array).
    NSMutable array does seem to support this method, however in the documentation it said that mutable arrays expand as needed.

    Yes. initWithCapacity isn't a way to create an array of nil pointers; it just provides a way for you to give NSMutableArray an estimate of what to expect. Presumably the implementation will do things differently if it's expecting a million objects instead of 10 or 20.
    (especially in the case of using integers, since I'd have to cast them back an forth to NSInteger objects).

    I think you might have meant NSNumber objects, and in that case you're correct. If you intend to populate an NSArray object with anything that isn't an object pointer, the data needs to be wrapped in an obj-C object. Both NSValue and NSNumber are good choices for that purpose.
    I just wondered what the recommended way to allocate fixed-size whose size is not known until runtime is with objective C (whether you should use C-style malloc, NSMutableArrays, etc.).

    If by obj-C, you mean Cocoa, Cocoa Touch or Foundation, the recommended way of storing data in an array is to use NSArray or NSMutableArray. AFAIK the SDK doesn't provide an array class which pre-allocates empty storage.

    And that said, are you sure you want such a thing? When I was limited to using calloc and malloc to reserve memory that might or might not be used, I would've given anything (within reason) for NSArray or NSMutableArray. The primary reason for their value is that they always work! I found out the hard way there's no such thing as realloc that always works (at least not in the days I was young enough to fight with it). So if I ever needed to grow or shrink an array I made a new one and copied the data.
    (whether you should use C-style malloc, NSMutableArrays, etc.).

    If you really want a dynamic array of ints which supports empty elements (whether its size ever needs to change or not), use calloc and free.

    \- Ray
  • m_userName Level 1 Level 1 (0 points)
    Thanks, I'll use the NSArray then. It's just I couldn't see a way to just allocate the memory, (like initWithCapacity) and have default objects which I allocate at a later point. The only thing I could see with the API was to create some dummy objects myself and then use arrayWithObjects. I was just curious to see what other people do.

    P.S. Yes I did mean NSNumber not NSInteger -doh.
  • RayNewbie Level 5 Level 5 (6,810 points)
    m_userName wrote:
    The only thing I could see with the API was to create some dummy objects...

    Sure. That's what the [NSNull|http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/F oundation/Classes/NSNullClass/Reference/Reference.html%23//appleref/doc/uid/TP40003703] class is for. There's a good example of your idea in the [PageControl|http://developer.apple.com/library/ios/#samplecode/PageControl/Int roduction/Intro.html] sample app (see awakeFromNib in PhoneContentController.m). - Ray