1

i have a little question in relation to the free() function of C.

I allocate in a program a multidimensional array with this code :

char **newMatrix( int N ){
    int i,j;
    char **a = malloc(sizeof *a * N);
    if (a)
    {
      for (i = 0; i < N; i++)
      {
        a[i] = malloc(sizeof *a[i] * N);
      }
    }

at the end of the program the array is full of characters. So i do this to deallocate the memory.

void freeArray(char **a, int m){
    int i;
    for (i = 0; i < m; ++i) {
        free(a[i]);
    }
    free(a);
}

My question is , how can I really check if the free() function works well , and deallocate all the memory? I ask you because I have tried to print the matrix after the freeArray , and the result is that the values are still stored in the a[i][j] columns and rows .

Sorry if it will be a stupid question , i'm new of C programming!

  • `how can I really check if the free() function works well..` you mean it's not working well in your case? [see this will help you](http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work?rq=1) – Jayesh Bhoi Aug 27 '14 at 08:25
  • 1
    Your described print action post-`free()` invokes undefined behavior, accessing memory no longer rightfully owned by your program. Undefined behavior, by its very-description, has no *definition*, and thus your results of examination after `free()` are both useless and futile. Your posted code is correct (assuming `N` and `m` are equivalent), standard compliant, and well-formed. *Know that*. – WhozCraig Aug 27 '14 at 08:25
  • Once again, you cannot hold the ball in basketball. – Kerrek SB Aug 27 '14 at 08:25

4 Answers4

5

free does not mean that it will actually delete the memory! It will inform to the OS that I don't want this memory any more, use it for some other process!

You can certainly continue to use array a after calling free(a) and nothing will stop you. However the results will be completely undefined and unpredictable. It works by luck only. This is a common programming error called "use after free" which works in many programs for literally years without "problems" -- until it causes a problem.

There are tools which are quite good at finding such errors, such as Valgrind.

ams
  • 24,923
  • 4
  • 54
  • 75
Sathish
  • 3,740
  • 1
  • 17
  • 28
1

free will not clear the memory for you. It just marks it as available for the OS to reallocate somewhere else. People tend to assign NULL to a pointer after freeing to prevent accidental reuse. If you want to be sure what your code is doing, you could memset the allocated space to a known value before freeing. To be honest, just print out how much data you mallocate and make sure you free the same size of data from the same pointer.

Joe
  • 7,378
  • 4
  • 37
  • 54
0

free() will only remove the reference to memory pointed to, by the pointer passed to it.

Once a pointer is passed to free() and the free() returns a success(which always is..), the pointer should never be used.

To avoid this usage, which is a illegal reference, a pointer variable should always be assigned NULL after it is passed to free().

0

If I understood you correctly you want to check if:

  • You don't reuse memory after freeing it
  • You freed all of the memory

It's not possible to do it directly from within language - however there are tools which can help. For example Valgrind can check both of those things (and much more). If you are on Windows tools like UMHD can be helpful but I haven't found any freeware replacement (though see also "Is there a good Valgrind substitute for Windows?" question)

Community
  • 1
  • 1
Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61