0

I want to store a 2D array of NSObjects using C pointer arrays. I read another StackOverflow question which said that it's possible to do this as follows:

id myArray [10][10];

However I want to allocate the memory dynamically because I don't know how big the table will be before hand.

I understand how to create a 2D pointer array for standard C variable types but I don't know how to do it for the id type. If I were using an int, I'd do something like this:

int ** myArray = (int**) calloc (10, sizeof(int*));
for(int i = 0; i<10; i++) {
    myArray[i] = (int *) calloc(10, sizeof(int));
}

Any ideas how to do this with the id data type?

4
  • Also, if I do sizeof(id) it asks me if I want to use use __bridge which doesn't look right. Commented Jun 18, 2013 at 0:29
  • For your int example, the initial call should be a int **, using sizeof(int *). What you've got doesn't make much sense. Commented Jun 18, 2013 at 0:35
  • Thanks, but my real question is about specifically storing the id data type. Any ideas? Commented Jun 18, 2013 at 0:36
  • Interesting answer and related to what I'm trying to do: stackoverflow.com/questions/11155275/… Commented Jun 20, 2013 at 20:03

1 Answer 1

1

If you're using ARC (which you probably should be) creating a C array of id type objects is going to be more trouble than it's worth. You need to both calloc and free the array in the usual (non id way) and you also need to annotate the id objects like id __strong myArray = … The other thing that's really counterintuitive but is required to make sure the elements in myArray are deallocated correctly is to explicitly set each element of myArray to nil before you free myArray.

So anyway, it's a lot of trouble and there are several gotchas to work around. You should just use an NSMutableArray of NSMutableArrays. With the latest versions of llvm you can still access the arrays using "C style" syntax, like myArray[x][y] = someObject;.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm using a C array because this code needs check a large grid of numbers many times per second on the UI thread so efficiency's very important.
Are you sure NSMutableArray is really that inefficient? I mean, yea it'll probably be less efficient, being a general purpose kind of thing, than well written single purpose code using lower level APIs, but don't fall into a premature optimization trap. Anyway, like I said, you can do this. The code will look very similar to your non-id code snippet. You have to annotate the id types with the __strong keyword and you have to explicitly set your objects to nil, like myArray[i][j] = nil (for all i and j) before freeing your arrays, to make this work correctly under ARC.
I agree with the above; if speed is an issue then you probably don't want to be marshaling values into & out of NSObjects (in C array, NSArray's or otherwise).

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.