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);
}
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?