LinkedList - How to free the memory allocated using malloc

76,199

Solution 1

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.

Solution 2

I use something like this:

for (p = curr; NULL != p; p = next) {
    next = p->next;
    free(p);
}

Solution 3

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)

Solution 4

You traverse the list using the same logic as above. You save the curr->next pointer somewhere, free the curr struct and assign curr with the saved curr->next pointer

Share:
76,199
goldenmean
Author by

goldenmean

struct descriptionOf { int elligent_Developer; short list_of_proven_skills; long list_of_ambitions; long long int erest_in_various_technologies; double effort_in_achieving_skills_and_ambitions; float ing_innovator; char of_a_hands_on_doer; }goldenmean; Software Developer with work experience in areas of Video/Image processing and codecs,DSP and multimedia Systems,on DSP/Multicore processor architecures, for devices and applications in Consumer Electronics, Communications industry. Programming languages: C,C++,Matlab/Octave,Python,DSP or RISC assembly languages.

Updated on April 13, 2020

Comments

  • goldenmean
    goldenmean about 4 years

    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
       }
    
    
    }