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.