free() and struct
Ok all you pro's here's another question that's got me thinking and I can't find any answers in my books.
Lets say we have a struct defined below:
Now lets say I got me a pointer (lptr) pointing to any studentPtr, and lets also say that I want to free() said pointer.
typedef struct studentType
struct studentType *next
typedef student *studentPtr;
Now iff I use
Will that also free up the memory that was malloc'ed for those char pointers?
I have always been under the impression that when you use
free(), especailly on a struct, it will free() up everything, included the memory reserved within the struct.
If you're not following, let me know and I'll post the question and my answer (was on an exam) and where I lost points.
No, it won't. It will only free the memory allocated for the structure, not the memory allocated for whatever it's ponters point at.
If this were the case, you'd free your entire linked list by simply:
free( topNode );
This would be a "BadThing(TM)".
Dang, there goes my A.
But that's what happens when I start thinking too hard. That's it, no more thinking for me!
Thanx anyway Quzah