Thread: How do I free a singlely linked list?

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Question How do I free a singlely linked list?

    what is the proper looping method for freeing a single linked list?
    I've never seen this done and my attempt at it is crashing. I am sure that this can be done in a few lines of code.

    I tried this:
    Code:
    do
    {
        list=list->next;
        free(list);
    }
    while(list);
    Thanks.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Hi,
    You are freeing the list at free(list);
    but trying to access it at while that may be the problem.
    So please change the order which may solve the problem.


    do
    {
    free(list);
    list=list->next;

    }while(list);


    by
    A.Sebasti..

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    do
    {
    free(list);
    list=list->next;

    }while(list);

    So you call free on list and then you dereference the pointer you have just freed......... BANG!!

    try something like this...

    do
    {
    struct node* currnode;
    currnode=list->next; // save location of next node
    free(list);
    list=currnode;// repoint list at saved node position
    }while(list);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Thanks Stoned
    I'll give that piece of code a whirl.

    ;-)
    I used to smoke the ganja man. I would get up in the morning, have breakfast, and then go eat. That was many a year ago! I'm an old fart now.
    hehe
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  5. #5
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    yes, Stoned_Coder's solution would work just fine...
    the "struct node* currnode; " part should be outside the while loop though.

    cheers,
    I don't wait for the future; it comes soon enough.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Thanks stoned_coder,
    I forgot to make use of temp variable something like current node,
    which is must otherwise bang.....

    Thanks a lot

    By
    A.Sebasti..

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void free_list( Node *n ) 
    {
        if( n ) {
            free_list( n->next );
            free( n );
        }
    }
    Called:

    free_list( myList );

    Quzah.
    Last edited by quzah; 12-05-2001 at 04:30 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM