Why do we need free()
? Putting the head node as NULL
does similar work, doesn't it?
-
3No. It just sets a pointer to NULL. But it doesn't automatically frees the memory you have been allocating earlier. – rturrado Sep 11 '20 at 10:19
-
4Why do we need to sell the house? I can just erase the address on my phone, can't I? – user253751 Sep 11 '20 at 10:27
-
1You could use valgrind to see when your program fails to call `free()` often enough. It will be death memory reserved for your process till the process ends. – 12431234123412341234123 Sep 11 '20 at 11:01
-
See also https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc?rq=1 – 12431234123412341234123 Sep 11 '20 at 11:23
-
Freeing memory by derefetencing only works for a language with automatic destructors or garbage collectors or if you do your own memory accounting, if you allocated nodes and data you need to free each of them before destroying the pointer to it. – eckes Oct 03 '20 at 16:13
3 Answers
Why do we need free()?
The function free
is used to free a dynamically allocated memory.
Putting the head node as NULL does similar work, doesn't it?
Putting NULL in the pointer to the head node does not free all dynamically allocated memory in the list neither for the head node nor for other nodes. This leads to loosing the address of the first dynamically allocated node and as a result you will get numerous memory leaks because the memory becomes inaccessible (but not freed)..
Consider the following demonstrative program.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *p1 = malloc( sizeof( int ) );
*p1 = 10;
printf( "*p1 = %d\n", *p1 );
int *p2 = p1;
p1 = NULL;
printf( "*p2 = %d\n", *p2 );
free( p2 );
return 0;
}
Its output is
*p1 = 10
*p2 = 10
In the program there is dynamically allocated a memory for an object of the type int
and the address of the allocated memory was assigned to the pointer p1
.
After assigning NULL
to the pointer p1
the address of the allocated memory is not stored in this pointer any more.
So if the address was not assigned to the second pointer p2
then the address of the allocated memory would be lost forever and we could not free the memory.
Only due to having a copy of the address in the pointer p2
we can free the allocated memory.
Thus setting a pointer to NULL
only changes the value stored in the pointer. The allocated memory is not touched in any way.

- 301,070
- 26
- 186
- 335
They are not equivalent.
In C, if you explicitly allocated memory (e.g., by using malloc
), you need to explicitly free it (by using free
).
If you just assign head = NULL
, you won't be able to access the following elements in the linked list, but their memory will still be allocated - the process will still hold this memory, and you'll have a memory leak on your hand.

- 297,002
- 52
- 306
- 350
Because there is no garbage collector in C. This means you should collect your garbage yourself. Everything you allocated on the heap has to be deleted manually. Forgetting to do it is called memory leak.

- 59
- 5
-
3It doesn't answer the question. Someone who is asking "why do I have to call free" doesn't know what "collecting garbage" means. "You have to collect garbage yourself" is a meaningless sentence to them. – user253751 Sep 11 '20 at 11:28