"wanwan" <er*******@yahoo.com> writes:
if I have the following structure and do the following procedure, will
I properly free up all allocated memory of the struture at the end?
typedef struct {
double name;
double *point2sumthing1;
double *point2sumthing2;
} obj_t;
obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));
obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));
obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));
...(do some stuff)
...
free(obj1);
My concern is the two double pointers. I am not certain whether free()
treats them as part of the structure
As others have said, it doesn't.
As a matter of style, it's not necessary to cast the result of
malloc(); doing so can mask the error of forgetting to
"#include <stdlib.h>". Also, using an object size rather than a type
size in the argument to malloc() can avoid problems if you later
change the pointer type.
For example, your first malloc() call could look like this:
obj_t *obj1 = malloc(sizeof *obj1);
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.