c - Heap error when I try to deallocate memory -


in program, use 2d-matrice each cell represents structure composed of 2 doubles. @ end of program, deallocate memory used matrice. wrote double loop "for" that:

if (i != null) {     (i = 0; < nb_prev; i++)     {         (j = 0; j < nb_samples; j++)         {             free(&(i[i][j]));         }         free(&(i[i]));     } }  

but obtain heap error after second loop inside seconde "for". maybe have done mistake. me please ?

updated comments:

the memory allocated follows:

power_time **i;  = (power_vtime)malloc(sizeof(power_time*)*nb_prev);  if (i == null) exit(0);  (i = 0; < nb_prev; i++) {      i[i] = (power_time*)malloc(sizeof(power_versus_time)*nb_samples);      if (i_arriv[i] == null) exit(0);  } 

since i 2d matrix of structs (and not pointers structs), there shouldn't inner loop.

you need free(i) @ end.

basically, how deallocate memory should mirror image of how you've allocated it. have single malloc() followed 1d loop of malloc()s. mirror image 1d loop of free() followed single free() @ end.


Comments