2

I've got a problem. I am trying to free an array of pointers using a loop, but it does not work. Could somebody help me with that?

This is the allocation code:

void create1(A*** a, int size)
{
    *a = (A**) malloc(size* sizeof(A*));
    for (int i = 0; i < size; i++)
    {
        a[0][i] = (A*)malloc(size * sizeof(A));
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user76234
  • 43
  • 1
  • 5

1 Answers1

5

You have to do the reverse of what you have done when you have allocated the memory.

Free the element pointers in a loop and finally free the pointer to the array:

 void del(A** a, int size)
 {
    for (int i = 0; i < size; i++)
    {
        free(a[i]);
    }
    free(a);
} 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174