John wrote on 30/07/04 :
> In the course of an assignment, I learned the hard way that I shouldn't try
> to free a malloc'd member of a malloc'd structure after having freed that
> structure (i.e., free( structure ); free( structure->bufferspace ) ).
>
> My question is, if I free just the structure, will the (e.g.) bufferspace be
> freed implicitly, or do I have to (as I currently am) free the members
> first?
"All what have been done must be undone".
IOW, yes, you have to free the innermost elements first. There is no
implicit automatic mecanism that frees the memory in C.
That said, you can write a pair of function known as 'creator /
destructor' that helps to create / delete the objects properly and hide
some gory details the user is not supposed to deal with.
myobj_s *myobj_create (void);
void myobj_delete (myobj_s *this);
--
Emmanuel
The C-FAQ:
http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"