473,508 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Nov 15 '05 #1
11 1168

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


Nov 15 '05 #2
"wanwan" <er*******@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: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 15 '05 #3
thx for responding.

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

Nov 15 '05 #4
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.
Nov 15 '05 #5
"wanwan" <er*******@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!
Nov 15 '05 #6
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
Nov 15 '05 #7
"wanwan" <er*******@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) 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.
Nov 15 '05 #8
"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.
Nov 15 '05 #9

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.

Nov 15 '05 #10
ma*******@gmail.com wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #11
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++
Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
22959
by: disco | last post by:
I am working on this example from a book "C Primer Plus" by Prata 4th edition - p. 672. There is no erata on this problem at the publisher's website. 1) Is it a violation of copyright laws to...
11
2261
by: Rodrigo Dominguez | last post by:
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this...
5
3140
by: Amogh | last post by:
Hi, My question is related to setting freed pointers to NULL. After freeing a pointer: 1) Should the freeing routine also be responsible for setting the pointer to null? 2) Or, should the...
1
1267
by: rupinenisuresh | last post by:
hi i am using the linked list like this.how i will free this linked list. struct ipa__serv_s { char *serv_name; int port; }; typedef struct ipa__serv_s ipa__serv_t;...
171
4722
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
6
2021
by: Praetorian | last post by:
This is actually 2 questions: The first one: I have a function (FuncA) calling another (FuncB) with a set of parameters which also includes an int * initialized to NULL before the call. Now...
11
1978
by: vivek | last post by:
Hello, I have a pointer to a main structure which again consists of structures, enums, char, int, float and again complex structures. When i free all the contents of the main structure, it...
25
4680
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.