Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Declaring fixed size array whose size is not known until runtime

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

Posted on Oct 21, 2010 7:11 AM

Reply
Question marked as Best reply

Posted on Oct 21, 2010 7:30 PM

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
3 replies
Question marked as Best reply

Oct 21, 2010 7:30 PM in response to m_userName

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

Oct 22, 2010 1:01 AM in response to RayNewbie

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.

Oct 22, 2010 2:47 AM in response to m_userName

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/NSNull Class/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

Declaring fixed size array whose size is not known until runtime

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.