freeing structure

W

wanwan

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


Thanks
 
M

manoj1978

wanwan said:
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
This approach doesnt work.
You have to free the double pointers seperately.
like,
free(obj1->point2sumthing1);
free(obj1->point2sumthing2);
free(obj1)
Otherwise there will be memory leak
 
B

Ben Pfaff

[three malloc()s, one free()]
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?

No. In general malloc() and free() should be paired. If you get
a pointer from malloc(), then you need to free() it yourself.
 
W

wanwan

thx for responding.

How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?
 
B

Ben Pfaff

wanwan said:
How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?

free() of a null pointer is a no-op.
 
M

Michael Mair

wanwan said:
thx for responding.

How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?

free() can have a null pointer as argument;
free(NULL);
has no detrimental effect whatsoever.

Cheers
Michael
 
D

Default User

wanwan said:
thx for responding.

How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?

No, it will not cause a problem.

Please review the FAQ list, both your questions are covered there.




Brian
 
K

Keith Thompson

wanwan said:
thx for responding.

How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?

Your system should have documentation for the free() function. It
should tell you that free(NULL) does nothing, and is not an error.
 
K

Keith Thompson

wanwan said:
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);
 
M

manoj1978

wanwan said:
thx for responding.

How about if I do free(obj1->point2sumthing1) while it points to NULL?
Will it give me a runtime error?
Passing a null pointer to free should cause no action.
Still its a better idea to avoid that since some compilers may have
issues with that.
 
C

Christopher Benson-Manica

Still its a better idea to avoid that since some compilers may have
issues with that.

If an implementation has a problem with free( NULL );, then it is not
conforming, and there is no end to what may also not go as expected.
 
E

Emmanuel Delahaye

wanwan wrote on 25/08/05 :
typedef struct {
double name;

double *point2sumthing1;
double *point2sumthing2;

} obj_t;

obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));

What the hell... Be simple :

obj_t *obj1 = malloc (sizeof *obj1);
obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));

obj1->point2sumthing1 = malloc(5 * sizeof *obj1->point2sumthing1);
obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));

My guess : obj1->...

obj1->point2sumthing2 = malloc(10*sizeof *obj1->point2sumthing2);
free(obj1);

My concern is the two double pointers. I am not certain whether free()
treats them as part of the structure

free() knows nothing about your structure.

No magic. If you have n malloc()'s, you must have n free()'s.

Don't bother with these gory details in your application code. I highly
recommend the constructor/destructor strategy :

/* create the stuff */
struct stuff *p = stuff_create(<eventually, configuration
parameters>);

if (p != NULL)
{
/* use the stuff */

/* when finished : terminate the stuff */
stuff_delete (p), p = NULL;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top