• 01-20-2005
franziss
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)?

• 01-20-2005
Brian
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.
• 01-20-2005
quzah
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.
• 01-20-2005
franziss
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?
• 01-20-2005
quzah
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.
• 01-20-2005
SMurf
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? :eek:

(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).
• 01-21-2005
franziss
Quote:

Originally Posted by SMurf
What? How much time do you think it takes to call a function and access a pointer? :eek:

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