32

I have the following code:

int **ptr = (int **)malloc(sizeof(int*)*N); 
for(int i=0;i<N;i++) 
     ptr[i]=(int*)malloc(sizeof(int)*N));

How can I free ptr using free? Should I loop over ptr and free ptr[i] or should I just do

free(ptr) 

and ptr will be freed?

deralbert
  • 856
  • 2
  • 15
  • 33
lina
  • 1,679
  • 4
  • 21
  • 25
  • This is not about free, but I wanted to talk about malloc. As Srikanth has pointed out, it's good if you check the malloc failure. Please refer to this material: https://stackoverflow.com/questions/27451220/how-can-i-correctly-handle-malloc-failure-in-c-especially-when-there-is-more-th –  Sep 09 '19 at 23:47

6 Answers6

28

Just the opposite of allocation:

for(int i = 0; i < N; i++)
    free(ptr[i]);
free(ptr);
Donotalo
  • 12,748
  • 25
  • 83
  • 121
25

You will have to loop over ptr[i], freeing each int* that you traverse, as you first suggest. For example:

for (int i = 0; i < N; i++)
{
    int* currentIntPtr = ptr[i];
    free(currentIntPtr);
}
Amal K
  • 4,359
  • 2
  • 22
  • 44
James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • 3
    Out of curiosity, what's wrong with just going through the loop with `free(ptr[i])`? – Russbear Aug 17 '11 at 17:02
  • 5
    Nothing, I may be trying a bit too hard to make my code readable here! – James Bedford Aug 18 '11 at 21:47
  • Hi James, I tried doing it the way you've written and my program breaks. _crtheap 0x0083000 void * pUserData 0x0083c408 const void * Why do you think this happens? Basically it's a heap block modified past requested size, something like that! – Rakshit Kothari Feb 26 '14 at 01:09
  • @JamesBedford - Sorry I couldn't tag you on the previous one. – Rakshit Kothari Feb 26 '14 at 01:27
  • @RakshitKothari - I'm guessing it's the free where it's crashing? The most obvious answer would be that your value for N is greater than the size of your array referenced to by `ptr`. – James Bedford Feb 26 '14 at 12:04
  • @JamesBedford Why did you not just use free(ptr[i]) inside the loop ? – Varad Bhatnagar Mar 22 '18 at 12:43
  • 1
    Just to more clearly lay out what's happening. It might also make it easier to set a breakpoint and see the value of pointer thats about to be freed, if that's of interest to the developer. – James Bedford Mar 23 '18 at 17:00
11

Yes, you must loop over ptr and free each ptr[i]. To avoid memory leaks, the general rule is this: for each malloc(), there must be exactly one corresponding free().

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
6

Simple

while (N) free(ptr[--N]);
free(ptr);

Handsome

#define FALSE 0
#define TRUE 1
typedef int BOOL;

void freev(void **ptr, int len, BOOL free_seg) {
    if (len < 0) while (*ptr) {free(*ptr); *ptr++ = NULL;}
    else while (len) {free(ptr[len]); ptr[len--] = NULL;}
    if (free_seg) free(ptr);
}

freev(ptr, N, TRUE); /* if known length */
freev(ptr, -1, TRUE); /* if NULL-terminated */
freev(ptr, -1, FALSE); /* to keep array */

Patrician

GLib functions:

I find it hard to do any serious C programming without GLib. It introduces things such as dynamic strings and lays foundations for functional programming. It should really be part of the standard C run-time library. It would give C a breath of fresh air. It would make C a reasonable and competitive language again for the year 2019. But because it isn’t, it will add 1 MB to your application (either in DLL size or in executable size). Also the Windows distribution is maintained by sadists.

7vujy0f0hy
  • 8,741
  • 1
  • 28
  • 33
1
for(int i=0;i<N;i++) free(ptr[i]);
free(ptr);

you are not checking for malloc failure to allocate. You should always check.

Srikanth
  • 11,564
  • 3
  • 23
  • 28
0

void freeMatrix(int **matrix ,int row)
{
    for(int i=0;i<row;i++)
    {
        free(matrix[i]);
    }
    free(matrix);
}