how to check the function free()

Q

questions?

I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

Sorry, I cannot post my huge program here.

Thanks for any comments
 
C

Chad

questions? said:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

Sorry, I cannot post my huge program here.

Thanks for any comments

Sir,

This is comp.lang.c, not comp.lang.c.IhaveESPandCanReadYourCodeByMagic
forum. Please post the entire code.

Thank You
 
S

Simple Simon

(e-mail address removed) wrote...
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing.

Why on earth did you do that?
Why I cannot free them?

free() means the allocation is no longer a valid reference. Not that
the values it contained have been re-initialized in some way.
 
K

Keith Thompson

questions? said:
I have a linked list of structures with pointers point to each other.
I go through the list and use free() to free the structures one by one
previous allocated by malloc(). After I did the whole thing. I tried
to print the list again, it has exactly the same thing. Why I cannot
free them?

What makes you think you can't free them? What did you expect to
happen when you tried to access free()d memory?

The free() function, quoting the standard, "causes the space pointed
to by ptr to be deallocated, that is, made available for further
allocation." Once the space is deallocated, any attempt to access it
causes undefined behavior. This means that, as far as the standard is
concerned, literally anything can happen -- including the exact
behavior you happened to see.

In your case, what *probably* happened is that the system marked the
previously allocated memory as available for further allocation, but
didn't clobber the memory itself (there's no real reason why it
should). Physically, the memory is still there, and still contains
whatever information you stored in it. But there's no guarantee that
it will stay that way. Somethinge else *could* come along and store
values in that memory before you print it -- or your program could
return it to the operating system, so that any attempt to access it
again could cause a trap -- or demons could fly out of your nose.

When you call free(), you're telling the system that you're finished
with the memory. If you're trying to access it again, you obviously
were't finished with it. If you lie to the implementation, it will
get its revenge (possibly by letting you get away with something like
this).

Just don't do that.
 
K

Keith Thompson

Chad said:
Sir,

This is comp.lang.c, not comp.lang.c.IhaveESPandCanReadYourCodeByMagic
forum. Please post the entire code.

Thank You

Actually, the question can be answered without seeing the code. See
my other response in this thread.
 
C

Chad

Keith said:
Actually, the question can be answered without seeing the code. See
my other response in this thread.

Interesting. I think this has to be the first thread I got more insight
into using free() without actually seeing any kind of working code.

Chad
 
Q

questions?

Keith said:
What makes you think you can't free them? What did you expect to
happen when you tried to access free()d memory?

The free() function, quoting the standard, "causes the space pointed
to by ptr to be deallocated, that is, made available for further
allocation." Once the space is deallocated, any attempt to access it
causes undefined behavior. This means that, as far as the standard is
concerned, literally anything can happen -- including the exact
behavior you happened to see.

In your case, what *probably* happened is that the system marked the
previously allocated memory as available for further allocation, but
didn't clobber the memory itself (there's no real reason why it
should). Physically, the memory is still there, and still contains
whatever information you stored in it. But there's no guarantee that
it will stay that way. Somethinge else *could* come along and store
values in that memory before you print it -- or your program could
return it to the operating system, so that any attempt to access it
again could cause a trap -- or demons could fly out of your nose.

When you call free(), you're telling the system that you're finished
with the memory. If you're trying to access it again, you obviously
were't finished with it. If you lie to the implementation, it will
get its revenge (possibly by letting you get away with something like
this).

Just don't do that.


Thanks for the nice comments.
Thank you all. I posted my code on another thread.
 
R

Richard G. Riley

Interesting. I think this has to be the first thread I got more insight
into using free() without actually seeing any kind of working code.

Chad

Since this thread was about something that happens under the covers,
what code could show anything in this threads context? It was as
simple as "dont access memory you have not legally got a handle to"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top