Thread: Freeing a doubly (circular) linked list

  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
    28,413
    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.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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