1

I want to use an array in all methods of my class. The array is initialized in the init method of the class.

But the size of the array is first known in the init method. E.g. in my init method I have:

CGPoint mVertices[size][size];

later in init I fill the array and in another method I read the values. How can I declare the array globally?

1 Answer 1

3

Make it an ivar:

@interface myClass : NSObject {
  CGPoint *mVertices;
}

@end

In your init method:

mVertices = malloc(size * size * sizeof(CGPoint));
if (!mVertices) { return nil; }

In your dealloc method:

free(mVertices); mVertices = NULL;
Sign up to request clarification or add additional context in comments.

2 Comments

just out of curiosity. Would you access this with mVertices[i][j] or with mVertices[i*size+j]?
Both are possible, but the [][] would most likely require multiple allocations.

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.