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?

James Andrews
  • 3,257
  • 3
  • 31
  • 45
  • Also, if I do sizeof(id) it asks me if I want to use use __bridge which doesn't look right. – James Andrews Jun 18 '13 at 00:29
  • For your `int` example, the initial call should be a `int **`, using `sizeof(int *)`. What you've got doesn't make much sense. – sapi Jun 18 '13 at 00:35
  • Thanks, but my real question is about specifically storing the id data type. Any ideas? – James Andrews Jun 18 '13 at 00:36
  • Interesting answer and related to what I'm trying to do: http://stackoverflow.com/questions/11155275/c-style-array-of-pointers-to-objective-c-objects-under-arc – James Andrews Jun 20 '13 at 20:03

1 Answers1

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;.

Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
  • 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. – James Andrews Jun 18 '13 at 00:47
  • 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. – Aaron Golden Jun 18 '13 at 01:00
  • 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). – geowar Jun 18 '13 at 16:03