easy question about free()

M

Michael

Hi,

if I have declared:

typedef struct something_big_struct Something_Big;

struct something_big_struct{
<lots of stuff>
Something_Big *a_big_thing;
};

and I allocate some memory:

SomethingBig *bigPtr;

bigPtr = malloc(sizeof(Something_Big));

After I'm finnished with bigPtr do I need to free a_big_thing separately?
ie do I need to free(bigPtr -> a_big_thing) before I free(bigPtr)?

Thanks for your help

Regards

Michael
 
B

Barry Schwarz

Hi,

if I have declared:

typedef struct something_big_struct Something_Big;

struct something_big_struct{
<lots of stuff>
Something_Big *a_big_thing;
};

and I allocate some memory:

SomethingBig *bigPtr;

bigPtr = malloc(sizeof(Something_Big));

After I'm finnished with bigPtr do I need to free a_big_thing separately?
ie do I need to free(bigPtr -> a_big_thing) before I free(bigPtr)?

If a_big_thing contains a value that was produced by m/c/realloc that
has not yet been freed, then you need to free it *at some point*. If
a_big_thing is the only pointer containing this value, then you do
need to free it before freeing bigPtr or you will have a memory leak.


Remove del for email
 
J

Jack Klein

Hi,

if I have declared:

typedef struct something_big_struct Something_Big;

struct something_big_struct{
<lots of stuff>
Something_Big *a_big_thing;
};

and I allocate some memory:

SomethingBig *bigPtr;

bigPtr = malloc(sizeof(Something_Big));

The preferred way to write this is:

bigPtr = malloc(sizeof *bigPtr);

....that way if you ever change bigPtr to point to another type, say
another structure that contains a struct something_big_struct and some
other members, you won't need to track down all the
sizeof(Something_Big) usages and edit them.

After I'm finnished with bigPtr do I need to free a_big_thing separately?
ie do I need to free(bigPtr -> a_big_thing) before I free(bigPtr)?

That depends, because you didn't show us how you provided memory to
bigPtr->a_big_thing.

If you got if from malloc() or calloc() after you allocated memory for
bigPtr, then you need to free() it BEFORE you free bigPtr.

Always one call to free() for every call to malloc() or calloc(). And
if you nest allocations, as you have above, you need to free() them in
reverse order.
 
R

Robert Gamble

Michael said:
Hi,

if I have declared:

typedef struct something_big_struct Something_Big;

struct something_big_struct{
<lots of stuff>
Something_Big *a_big_thing;
};

and I allocate some memory:

SomethingBig *bigPtr;

bigPtr = malloc(sizeof(Something_Big));

After I'm finnished with bigPtr do I need to free a_big_thing separately?
ie do I need to free(bigPtr -> a_big_thing) before I free(bigPtr)?

Every malloc/calloc/realloc should have a corresponding free(). If you
malloc'ed bigPtr->a_big_thing (which you didn't in your example) then
you need to free it seperately and you must do so before freeing
bigPtr, failing to do so will cause a memory leak. Of course if you
never assigned bigPtr->a_big_thing to point to dynamically allocated
memory you must not free it (unless its value is NULL in which case it
would be safe, but unnecessary to do so).
Note that if another part of your program is still using the memory
pointed to by bigPtr->a_big_thing (if you have multiple pointers
pointing to the same memory), you must only free it one time and you
would do so after your program no longer needs access to it.

Robert Gamble
 
K

Kevin Handy

Michael said:
Hi,

if I have declared:

typedef struct something_big_struct Something_Big;

struct something_big_struct{
<lots of stuff>
Something_Big *a_big_thing;
};

and I allocate some memory:

SomethingBig *bigPtr;

bigPtr = malloc(sizeof(Something_Big));

After I'm finnished with bigPtr do I need to free a_big_thing separately?
ie do I need to free(bigPtr -> a_big_thing) before I free(bigPtr)?

You need to free everything that has been malloc, calloc, etc.
seperately.

You will need to free 'bigPtr -> a_big_thing' before freeing
the 'bigPtr' structure, because as soon as you free 'bigPtr',
you can no longer trust what it is pointing to. None of the
'bigPtr' elements should be accessed after that. The 'free'
specifies that you are done with it. The structure may be
overwritten before you access the 'a_big_thing' member, causing
inconsistant hard to debug errors.
 
H

Herbert Rosenau

Hi,

if I have declared:

typedef struct something_big_struct Something_Big;

struct something_big_struct{
<lots of stuff>
Something_Big *a_big_thing;
};

and I allocate some memory:

SomethingBig *bigPtr;

bigPtr = malloc(sizeof(Something_Big));

After I'm finnished with bigPtr do I need to free a_big_thing separately?
ie do I need to free(bigPtr -> a_big_thing) before I free(bigPtr)?

There is an easy answer:

Anything you've requested with malloc() needs a free(). When you
stored the pointer you've got from malloc() in a nested form like a
linked list and into its members you have to free() the malloced data
of a list member before you can free the member itself. Else you'll
get memory leaks.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top