Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinkedList - How to free the memory allocated using malloc

I have a very simple C code for constructing a Singly Linked list as below, in which I allocate memory for each node dynamically using malloc. At the end of code, I want to free the memory for each node allocated, was wondering how to go about it - If I start from head node first and free it, the pointers to the subsequent nodes are lost and memory leak happens.

Other way is start from head node and keep storing the node pointer in a separate array of pointers or something, traverse the list till the tail pointer while storing the node pointers, and once reach the tail node, store that also to the other array of pointers and start freeing from that array index backwards until the head node is free'ed.

Is that the only way to achieve what I am trying to do?

In case if I dont want to use second buffer, how do I go about it.

#include "stdio.h"
#include "stdlib.h"

struct lnk_lst 
{
   int val;
   struct lnk_lst * next;
};

typedef struct lnk_lst item;


main()
{
   item * curr, * head;
   int i,desired_value;

   head = NULL;

   for(i=1;i<=10;i++) 
   {
      curr = (item *)malloc(sizeof(item));
      curr->val = i;
      curr->next  = head;
      head = curr;
   }

   curr = head;


   while(curr) {
      printf("%d\n", curr->val);
      curr = curr->next;
   }

  //How to free the memory for the nodes in this list?
   for(i=1;i<=10;i++)
   {
       free()//?? What logic here
   }


}
like image 491
goldenmean Avatar asked Aug 11 '11 11:08

goldenmean


People also ask

How do I free the memory allocated by malloc?

3.2. 3.3 Freeing Memory Allocated with malloc When you no longer need a block that you got with malloc , use the function free to make the block available to be allocated again. The prototype for this function is in stdlib. h .

How do you free allocate memory in a linked list?

We can use the loop to iterate over the linked list and free the allocated memory for each node. Remember, we cannot expect the whole structure of a linked list to be freed by calling the free() function from the root node.

Do I need to free memory after malloc?

But the free() method is not compulsory to use. If free() is not used in a program the memory allocated using malloc() will be de-allocated after completion of the execution of the program (included program execution time is relatively small and the program ends normally).

Why we use malloc in linked list?

It allocates the required memory to the compiler at runtime to use and program works well.


3 Answers

The usual way is with (pseudo-code first):

node = head              # start at the head.
while node != null:      # traverse entire list.
    temp = node          # save node pointer.
    node = node.next     # advance to next.
    free temp            # free the saved one.
head = null              # finally, mark as empty list.

The basic idea is to remember the node to free in a separate variable then advance to the next before freeing it.

You only need to remember one node at a time, not the entire list as you propose.

In terms of what you need to add to your code, you can, during deletion, use head as the continuously updating list head (as it's meant to be) and curr to store the item you're currently deleting:

while ((curr = head) != NULL) { // set curr to head, stop if list empty.
    head = head->next;          // advance head to next element.
    free (curr);                // delete saved pointer.
}

This is a little shorter than the pseudo-code above simply because it takes advantage of C "shorthand" for some operations.

like image 69
paxdiablo Avatar answered Oct 21 '22 13:10

paxdiablo


I use something like this:

for (p = curr; NULL != p; p = next) {
    next = p->next;
    free(p);
}
like image 37
cnicutar Avatar answered Oct 21 '22 12:10

cnicutar


Your free code should be as follows:

lnk_lst temp = null;
while(head) 
{
  temp = head->next;
  free(head);
  head = temp;
}

Also I would like to add after your malloc you probably want to check whether the mem was allocated successfully.. something like

if(curr)
like image 2
Baz1nga Avatar answered Oct 21 '22 14:10

Baz1nga