Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   freeing structure (http://www.velocityreviews.com/forums/t439197-freeing-structure.html)

wanwan 08-25-2005 07:39 PM

freeing structure
 
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


manoj1978@gmail.com 08-25-2005 07:48 PM

Re: freeing structure
 

wanwan wrote:
> 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


>
>
> Thanks



Ben Pfaff 08-25-2005 07:49 PM

Re: freeing structure
 
"wanwan" <ericwan78@yahoo.com> writes:

[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.
--
Ben Pfaff
email: blp@cs.stanford.edu
web: http://benpfaff.org

wanwan 08-25-2005 07:52 PM

Re: freeing structure
 
thx for responding.

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


Ben Pfaff 08-25-2005 08:04 PM

Re: freeing structure
 
"wanwan" <ericwan78@yahoo.com> writes:

> 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.
--
Here's a tip: null pointers don't have to be *dull* pointers!

Michael Mair 08-25-2005 08:05 PM

Re: freeing structure
 
wanwan wrote:
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

Default User 08-25-2005 08:10 PM

Re: freeing structure
 
wanwan wrote:

> 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

Keith Thompson 08-25-2005 08:18 PM

Re: freeing structure
 
"wanwan" <ericwan78@yahoo.com> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@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.

Keith Thompson 08-25-2005 08:22 PM

Re: freeing structure
 
"wanwan" <ericwan78@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) kst-u@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.

manoj1978@gmail.com 08-25-2005 09:01 PM

Re: freeing structure
 

wanwan wrote:
> 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.



All times are GMT. The time now is 04:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.