Malax
Malax

Reputation: 9604

Array memory management (Not NSArray, but type[23])

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?

Upvotes: 0

Views: 73

Answers (3)

JeremyP
JeremyP

Reputation: 86651

CGPoint is a C struct type. The declaration allocates the memory directly on the stack. In C you can pass structs directly as values. CGPointMake() probably looks something like this:

struct CGPoint {
     CGFloat x;
     CGFloat y;
};
typedef struct CGPoint CGPoint;

 CGPoint CGPointMake(CGFloat x, CGFloat y)
 {
     CGPoint returnValue; // allocated directly on the stack
     returnValue.x = x;   // Do not confuse this with Objective-C dot notation.  It is NOT the same thing
     returnValue.y = y;
     return returnValue;
 }

That's perfectly legal and the entire struct is copied off the stack as the return value.

Bonus Question: How does this compare to NSArrays with NSValues? Is it faster?

Try it and see, but I'll tell you that the answer is a definite yes. There's no Objective-C message passing to do.

Upvotes: 1

Stephen Darlington
Stephen Darlington

Reputation: 52565

The statement CGPoint manyPoints[40]; allocates 40 CGPoints on 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:

CGPoint* manyPoints = malloc(40 * sizeof(CGPoint);

// blah

manyPoints[0] = CGPointMake(23, 42);

// I'm done now
free(manyPoints);

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.

Upvotes: 1

justin
justin

Reputation: 104698

right - CGPoint is just a C struct.

Where is the memory allocated?

as declared, on the stack.

if you'd used malloc, then it would be on the heap (unless optimised out).

When is it deallocated?

as declared, when the scope exits, the region is available for reuse on the stack.

if you'd malloced it, you'd have to free it.

What happens when i pass it around? Is it copied?

it depends on how you pass it. the language provides the option to pass by value and by reference.

I cannot find anything on this topic in regard to ObjectiveC, but Im not sure where to look instead.

a C book

Bonus Question: How does this compare to NSArrays with NSValues? Is it faster?

stack allocations and objects are waaaaaay faster, although they can be misused and there are good uses for all.

Upvotes: 1

Related Questions