Subj : Re: Memory Pointers and Free To : borland.public.cpp.borlandcpp From : =?ISO-8859-15?Q?Jo=E3o?= Paredes Date : Sun Jul 25 2004 02:27 am Alistair wrote: > If I have a structure e.g. > > struct s_a > { > char *somedata; > }; > > I then do the following : > > s_a *a = malloc(sizeof(s_a)); > > a->somedata = malloc(512); > > free(a); > > what happens to a->somedata, is that freed too? Or because it holds a > different memory address it isnt free and its left hanging? (memory leak!?) > > Cheers > > Al No, it would remain reserved or allocated but unreachable. That is one of the causes for the effect known as memory leak. Before freeing the structure, you first have to free all malloc-allocated elements in the structure recursively. Also, with the declaration for the structure and variable you posted, I don't believe that would even compile. If you create a type with "struct something {...};" you would have to declare variables with "struct something myvar;". If you don't want to use "struct something" everytime you create a type with that structure, declare your structure this way (following your example): typedef struct s_a { char *somedata; }; Then you can do it like this: s_a *a = malloc(sizeof(s_a)); .