I have a function which is called a lot of times during the execution. Inside this function I allocate an array:
double **MUDG_table;
//dynamic allocate array of MUDG_table (1st Dimension)
MUDG_table = calloc(arr_row,sizeof(double *));
//check if the memory has been allocated correctly
if (MUDG_table==NULL)
{
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
}
for (cv02=0;cv02<arr_row;cv02++)
{
MUDG_table[cv02] = calloc(arr_column, sizeof(double));
//check if the memory has been allocated correctly
if (MUDG_table[cv02]==NULL)
{
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
}
}
When I finish with the the calculations and before returning the value I want I try to free the memory:
//free memory
for (cv02=0;cv02<arr_row;cv02++)
{
free(MUDG_table[cv02]);
}
free(MUDG_table);
and it crashes.
If I remove it, it works a few times (as I said the function is called several times in a loop with different arguments each time) and then it crashes. Any ideas?