I’ve just started development of iOS Applications and my question relates to simple arrays. My Application, which is a game, makes heavy use of the CGPoint struct. Because it is not an object, I cannot put it directly in an NSArray and have to wrap it within a NSValue. This works fine.
In other, mostly example code, I see the usage of simple arrays (i guess that’s C?):
CGPoint manyPoints[40];
manyPoints[0] = CGPointMake(23, 42);
// And so on...
Where is the memory allocated? When is it deallocated? What happens when i pass it around? Is it copied? I cannot find anything on this topic in regard to ObjectiveC, but Im not sure where to look instead.
Bonus Question: How does this compare to NSArrays with NSValues? Is it faster?
The statement
CGPoint manyPoints[40];allocates 40CGPointson the stack which means that (in general) you can’t pass them around. When you leave the current stack frame (falls out of the current set of curly brackets) the data will automatically be deallocated.You could also do the following:
You’ll probably have more luck searching around C (rather that Objective C) for this.
And yes, it’ll almost certainly be quicker than using
NSArray/NSValue.