0

I'm trying to create and zero an array of ints based on a size that I get at runtime:

size = [gamePiece.availableMoves.moves count]; //debugger shows size = 1;
int array[size]; //debugger shows this as int[0] !
memset(array, 0, size);
indexes = array;

size and indexes are both ivars of this class:

int size;
int* indexes;

I end up with a 0-length array, though. How can I create it with the size indicated by [gamePiece.availableMoves.moves count]?

jscs
  • 63,694
  • 13
  • 151
  • 195
Mazyod
  • 22,319
  • 10
  • 92
  • 157
  • Variable length arrays are valid C99, but not C++ (though some compilers support it), which may be one reason why `int array[size]` didn't work when you tested it. Another potential reason is the debugger can't deal with variable length arrays. Not to say that the above would work under C99, for the reason others have pointed out, namely that `array` has local duration. See also: [C/C++: Array size at run time w/o dynamic allocation is allowed?](http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed). – outis Mar 01 '11 at 04:34

2 Answers2

6

First of all, you can't do what you're doing. Even when this works, the array is going to disappear when the method returns and the current stack frame is removed. You need to dynamically allocate the array, then you need to remember to free it when your object is deallocated. So:

size = [gamePiece.availableMoves.moves count];
indexes = calloc(size, sizeof(int));

Then, in your -[dealloc] method:

if( indexes ) free(indexes);

Using calloc(3) will ensure that all the memory is zeroed out, so you don't need to call memset(3).

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • You generally don't need to check the value for NULL before calling free(); I don't know of a single free() implementation that is not NULL-safe. – vanza Mar 01 '11 at 04:07
  • Am new to C, but does Mahesh's answer work? He uses malloc? is it the same as calloc? – Mazyod Mar 01 '11 at 04:09
  • @Mazyod - No, calloc assures that every memory location being returned is set to NULL while malloc doesn't – Mahesh Mar 01 '11 at 04:10
  • We have variable length arrays in gcc with C99, which is the default in Xcode. It's just that the debugger can't show the length. But that doesn't mean the array isn't the correct size. It will be on the stack, however. – Firoze Lafeer Mar 01 '11 at 04:32
  • @Firoze Lafeer - his problem wasn't the variable length array, it's that you can't assign it to an ivar, which is what he's after based on his question. – Jason Coco Mar 01 '11 at 05:29
  • @Jason Coco, yeah, sorry I didn't make that clear when I said it would be "on the stack, however". Was just trying to point out that the variable length array itself is possible. – Firoze Lafeer Mar 01 '11 at 06:15
2

Array size should be a constant integral expression. You need to use malloc.

int *array = malloc( sizeof(int) * size ) ;

Now, you can normally access elements by index operator [].

Mahesh
  • 34,573
  • 20
  • 89
  • 115