Thread: freeing list that gets nodes from block

  1. #1

    freeing list that gets nodes from block

    ok, lets say i have a function that gets nodes for a linked list from a malloc'ed/calloc'ed block of structs. lets also say that the function exhaust several blocks. after im finished doing whatever with the list, how do i free all the nodes. i dont think i could just traverse the list freeing each node because id end up freeing addresses that have already been freed (i think). i thought maybe i could either store the beginning address of each block allocated on a stack then traverse through that or maybe i could free the first node in the list, go down BLOCKSIZE nodes, free and repeat until end of list but im not sure which would be the best way to do it. im not even sure if thats the way it should be done.
    id really appreciate it if you guys could help me out with this. thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    What do you need help for? You've already suggested two solutions to your problem, have you tried them yet? To be perfectly honest the best way to learn both programming and problem solving is to fiddle around with any and all laternatives that you can think of.

    I know that that isn't the answer you wanted, but it is the answer you needed

    My best code is written with the delete key.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Well you can ALMOST free them sequentially. In the 'delete' function, use a temp variable as a placeholder like this:

    void DeleteList(Node *head)
    Node *me;
    Node *t = head;

    while(t->next != NULL)
    me = t;
    t = t->next;
    free(t); // Last node...
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. freeing list
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-31-2002, 06:45 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM