How to allocate 2D array dynamically?

I need to allocate a 2D array as a 1D continuous array with the following code,
double **allocate_2D_array(int row, int col)
{
// input: row, col number
// output: **arr
// purpurse: set a 2D array in the form a 1D array to facilitate
// both msg passing, and refering
int i;
double **arr;
double *tmp;
if((tmp = (double*)calloc(row*col, sizeof(double)))==NULL)
{
printf("Not enough memory !\n");
exit(1);
}
if((arr = (double**)calloc(row, sizeof(double *)))==NULL)
{
printf("Not enough memory !\n");
exit(1);
}

// assign the 2D array pointer
for(i=0; i<row; i++)
{
arr[i] = &tmp[col*i];
}

return arr;
}

And deallocate the array with
int deallocate_2D_array(double **arr)
{
free((void *)arr[0]);
free((void *)arr);

return 0;
}

My problem is: i can allocate and deallocate one array well, but if i want to allocate and deallocate for the second time, the deallocate fucntion does not work.

Any idea on why i can only allocate and deallocate once?

Thanks,

Hailiang
I go over some techniques here:

http://cplusplus.com/forum/articles/17108/

The article has a general bias because I hate MD arrays. But it does provide examples of how to construct and use them.
Topic archived. No new replies allowed.