I’m trying to mimic the following Java code:
int[][] multi; // DIMENSIONS ARE UNKNOWN AT CREATION TIME // LATER ON... multi = new int[10][]; multi[5] = new int[20]; multi[5][11] = 66; // LATER ON... multi = null; // PROPER WAY OF FREEING EVERYTHING
I’ve came out with the following Objective-C version:
int* *multi; // multi = malloc(10 * sizeof(int*)); multi[5] = (int *) malloc(20 * sizeof(int)); multi[5][11] = 66; // free(multi[5]); free(multi);
So first, I’d like to hear if it’s the best way to go. And mostly: I can’t find a way to free memory in some ‘automatic’ fashion, i.e. the following code is causing run-time exceptions on the IPhone:
for (int i = 0; i < 10; i++) { if (multi[i] != NULL) free(multi[i]); }
Free doesn’t zero out the memory address in the pointer, it just invalidates it. So, if you’re running this loop more than once, you will get exceptions when you try to free memory that has already been invalidated. You can use an NSPointerArray or wrap your integers in objects and use an NSMutableArray for your purposes, but if you just want to use what you have, and you’re running the loop more than once, you will have to do something like:
This way if the loop is run more than once, you won’t fail. Also, I use calloc instead of malloc because it will set all the pointers to NULL and integers to 0. The first parameter is the size of the array you want (in your case) and the second parameter is the size of the type (so no multiplication is required).