Deepak Yadav
Deepak Yadav

Reputation: 351

Freeing the head node of linked list

If I free the head node of the linked list would it just remove the head node with other nodes still in memory or it would free the entire list and how ?

Upvotes: 3

Views: 7143

Answers (4)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58281

Your linked list should be like:

head
+---+    +---+    +---+                               
| 1 |--->| 2 |--->| 3 |---+
+---+    +---+    +---+   |                                
                         null  

head node keeps address of fist node only, if you do free(head), then it will free memory of first node with value 1 only and other-nodes are still in memory and its valid to access them, but you should first save address of node 2, to access linked list (else you would have a memory leak in your code).

Do like:

   new_head = head->next;
   free(head);

Once you deallocate/free() a memory, its Undefined behavior to access that not (address becomes invalid).

From comment:

  1. Yes, you need a loop to free() memory for all nodes in linked-list, do something like this:

     while(head){          // while head not null
         new_head = head->next;   // first save address of next
         free(head);           // free first node
         head = new_head;      // set head to next node, not yet free
     }
    
  2. comment-2:
    If you don't delete/free dynamically allocated memory in your program then it will remain allocated to your process till it not terminates (remember in C we don't have Garbage collector). Dynamically allocated memory has life till your program does't terminate. So if you have finished your work with allocated memory, free it explicitly.

Upvotes: 6

Alex
Alex

Reputation: 10126

No, when you free the head node of the linked list, you only mark memory that it used as "free for use". So, other nodes in memory still will be marked as "in use".

Technically, you still be able to use freed memory, but it is undefined behavior, so there is no guarantees that data (including pointers) still correct for use.

If you want to delete all nodes from memory, you should do it recursively (from the next pointer point of view).

Upvotes: 0

tay10r
tay10r

Reputation: 4357

Freeing the head node of the list only frees that node, not the rest of the list.

This is the main difference between arrays and linked lists - the memory is separated. Here's an illustration:

 struct testnode {
      int a;
      int b;
      struct testnode * next;
 };

In an array, the memory may look like this:

 struct testnode test[3];

 0x............0000    &test[0] // on a typical computer
 0x............0020    &test[1]
 0x............0040    &test[2]

Notice how the elements in the array are all laid out right next to each other? Freeing test will free and the continous memory that follows, only one call is need to free all the memory.

In a linked list, the memory may look like this:

 struct testnode * head;

 // after insertions

 0x............0000    head
 0x............634f    head->next 
 0x............114d    head->next->next

Notice how the memory is unsorted (although it may not always be that out of order). And freeing head will only free the head node, leaving the rest of the memory untouched.

Upvotes: 1

manav m-n
manav m-n

Reputation: 11394

Freeing the head node would just free the memory allocated to the head node. The other list nodes would be retained. You have to then point head to the next member of the list. For example consider the following single linked list,

A -> B -> C -> D -> Null

After freeing the head node A, and moving the head to node B, the linked list would look like this

B -> C -> D -> Null

Upvotes: 1

Related Questions