Thread: linked list: properly freeing and reusing inside of loop??

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    linked list: properly freeing and reusing inside of loop??

    Hello all; I haven't programmed anything in ages, and could use some help trying to recall how to properly initialize, free, and then reuse the same variables from within a loop. I'm working with a single linked list defined as

    Code:
    typedef struct __LLIST {
       char *data;
       struct __LLIST *next;
    } list;
    my variables are declared as

    Code:
    list base = { 0, 0 }, *node = &base, *tmp = 0;
    Here's what I've tried; it *appears* to work on the first run but throws an exception on the next iteration of the control loop.

    Code:
    control loop {
       // prompt user for string
       // read string into static char array
       // split string using strtok: p = strtok(user_input, " ");
       
       while(p) {
          node->next = malloc(sizeof(*node));
          node = node->next;
          node->data = malloc(strlen(p) + 1);
          strcpy(node->data, p);
          node->next = 0;
          p = strtok(0, " ");
       }
    
       // do stuff with list
       // try to free and reuse it
    
       node = &base;
       tmp = 0;
    
       while(node) {
          tmp = node;
          free(node->data);
          node = node->next;
          free(tmp);
       }
    
       node = 0;
       tmp = 0;
    
       memset(&base, 0, sizeof(list));
       node = &base;
    }
    Any ideas/help would be greatly appreciated.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Lines 18, 22, and 25 amount to trying to call free on what must either be a global or local variable rather than a pointer.
    That will do bad things
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Thank you so much for the reply.

    It just hurts to see how much I've forgotten... lists always gave me fits and they still haunt me it seems. I was about to shelve this idea and try my hand at using a vector instead.

    Code:
        for( ; ; ) {
    
            node = &base;
            tmp = NULL;
        
            while(p) {
                // code unchanged
            }
    
            // *seems* to work or at least
            // runs without dying
    
            node = base.next;
    
            while(node) {
                // code unchanged
             }
        }
    Cheers.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing a doubly (circular) linked list
    By nkbxwb in forum C Programming
    Replies: 4
    Last Post: 10-09-2011, 05:14 PM
  2. need help freeing a linked list
    By Muster Mark in forum C Programming
    Replies: 2
    Last Post: 06-14-2011, 05:24 PM
  3. Freeing a linked list -- possible bug.
    By msh in forum C Programming
    Replies: 5
    Last Post: 01-25-2011, 12:22 PM
  4. Freeing a linked list
    By Matty_Alan in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2010, 11:19 PM
  5. Freeing a linked list
    By CeeCee in forum C++ Programming
    Replies: 7
    Last Post: 02-02-2002, 05:59 PM