The idea is to use only two functions that can both allocate and free a 2D array of a given data type that is not known at compile-time. The code I have written crashes in the freeing function, but I cannot see why. Here is the code:
void** new2DArray(int row, int col, int typeSize){
void** temp = malloc(sizeof(void*)*row); //EDIT
for (row-=1; row >= 0; row--)
temp[row] = malloc(typeSize*col);
return temp;
}
void free2DArray(void** array, int row, int typeSize){
for (row -= 1; row >= 0; row--)
free( *(array + row*typeSize) );
free(array);
}
Where, for a 3x3 array, is would be called like:
double** test = (double**) new2DArray(3, 3, sizeof(double));
free2DArray(test, 3, sizeof(double));
Is there anything immediately wrong with the freeing function?