Freeing a doubly (circular) linked list

This is a discussion on Freeing a doubly (circular) linked list within the C Programming forums, part of the General Programming Boards category; Hey, I have a circular linked list, and I'd like to fee the memory that I've malloc'd . I tried ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Freeing a doubly (circular) linked list

    Hey,

    I have a circular linked list, and I'd like to fee the memory that I've malloc'd .

    I tried doing this

    Code:
    listnode temp;
    listnode current=NULL;
    
    
    temp=head;
    while(temp!=NULL)
    {
    current=temp;
    current=current->previous;
    free(temp);
    temp=current;
    }
    But as a check to see if it works, I print the list after, and it prints perfectly, indicating that the memory was not cleared. What am I doing wrong?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by nkbxwb
    But as a check to see if it works, I print the list after, and it prints perfectly, indicating that the memory was not cleared. What am I doing wrong?
    That does not mean that memory was not cleared. It could mean that you are not removing the nodes correctly, or it could be a bug with the printing.

    To remove all the elements of a circular linked list, I might start at the head and then remove the next node until the head is reached. What exactly is your idea? (I'm not saying that you are wrong; I'm just too lazy to try to debug it step by step - which you should be doing - before I hear what you have to say.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    I send the printing function the head, and ask it to print. So printing out, I'd assume, means that the head was not cleared. I tried inserting head into that while loop, but I got a segfault. I believe this has to do with running off the end of the list. I'll change some parameters, and see if it works.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Well, you don't seem to actually assign to head, which could explain why head is not cleared, or rather, does not appear to be cleared.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also, you will never have 'temp == NULL' for your check because calling free doesn't set everything freed to NULL. So you will start double-freeing until it crashes.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  2. Circular doubly linked list with dummy header
    By mag_chan in forum C Programming
    Replies: 5
    Last Post: 10-31-2005, 08:44 AM
  3. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  4. Replies: 1
    Last Post: 02-24-2002, 06:22 AM
  5. Circular Linked list ....
    By yescha in forum C Programming
    Replies: 1
    Last Post: 11-17-2001, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21