Thread: Free link list

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    Free link list

    I have a link list (let's name it A) and each node of the link list contains some data fields. one of the data field is a link list (let's name it B)

    So if I call Free(A), will it also free B? Or do I have to call Free(B) first, then call Free(B)?

    Thanks for your help! =)

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    just do this:

    Code:
    node *tmp = a;
    a = a->next;
    free(tmp);
    Then you have your node B where node A used to be, and node A is freed.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It will free the pointer itself, not what is allocated at the pointer. Consider:
    Code:
    struct foo
    {
        int *bar;
    };
    ...
    
    struct foo *baz;
    baz = malloc( sizeof( *baz ) );
    baz->bar = malloc( sizeof( *(baz->bar) ) );
    
    /* incorrect: */
    free( baz ); /* What is allocated for "baz->bar" has not been freed. */
    
    /* correct, assuming you didn't just do the above line: */
    free( baz->bar );
    free( baz );
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Quote Originally Posted by quzah
    /* correct, assuming you didn't just do the above line: */
    free( baz->bar );
    free( baz );
    Quzah.
    I am doing this but it takes up more computation time. I'm thinking if I just call free(baz), there will not have pointer pointing to the memory space of bar. So what happen to this memory space? Will the Operating system be smart enough to take baz memory space as free space and overwrite them? Or it will take it as allocated space and this space cannot be used?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by franziss
    I am doing this but it takes up more computation time. I'm thinking if I just call free(baz), there will not have pointer pointing to the memory space of bar. So what happen to this memory space? Will the Operating system be smart enough to take baz memory space as free space and overwrite them? Or it will take it as allocated space and this space cannot be used?
    Tough. It's your job to free everything you allocate. You cannot rely on the OS to do the dirty work for you. You allocate it, you free it. That's just the way it goes.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Quote Originally Posted by franziss
    I am doing this but it takes up more computation time.
    What? How much time do you think it takes to call a function and access a pointer?

    (Assuming, when under Visual C++, that you're doing a release build and not debug. I'm aware that the debug heap is a fair bit slower, but it does do a lot of bounds checking).

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    Quote Originally Posted by SMurf
    What? How much time do you think it takes to call a function and access a pointer?
    Well, my nodes are in millions, n i'm using a 1ghz amd with 256mb ram.
    I'm using while loop to free them up. when i free them up, my program slows by 1-2sec. I'm doing an experiment, comparing my algo with others. This 1-2 sec do makes a difference to me. But I think if my nodes size increases, not freeing the nodes will slow down my program considerably

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM