freeing list that gets nodes from block

This is a discussion on freeing list that gets nodes from block within the C Programming forums, part of the General Programming Boards category; ok, lets say i have a function that gets nodes for a linked list from a malloc'ed/calloc'ed block of structs. ...

  1. #1
    Unregistered
    Guest

    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
    Posts
    9,796
    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

    -Prelude
    My best code is written with the delete key.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    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(me);
    }
    free(t); // Last node...
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

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, 12: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, 05:45 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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