free()'ing structs containing pointers to memory? 
Author Message
 free()'ing structs containing pointers to memory?

What happens to memory referenced by pointers which are part of a
struct, when that struct is freed?

for example if I ...
------------------------------------------------------
typedef struct
{
        char foo[27];

Quote:
} FOOSTRUCT;

typedef struct
{
        char bar[27];
        FOOSTRUCT *x;

Quote:
} BARSTRUCT;

BARSTRUCT *bar=calloc(sizeof(BARSTRUCT),1);
... code involving bar ...
free(bar);
---------------------------------------------------------------
Is there still memory allocated somewhere for bar->x?

In other words, does this constitute a memory leak, requiring me
to first free(bar->x) before I free(bar)?



Tue, 27 Apr 1999 03:00:00 GMT  
 free()'ing structs containing pointers to memory?

Quote:

>What happens to memory referenced by pointers which are part of a
>struct, when that struct is freed?

>for example if I ...
>------------------------------------------------------
>typedef struct
>{
>        char foo[27];
>} FOOSTRUCT;

>typedef struct
>{
>        char bar[27];
>        FOOSTRUCT *x;
>} BARSTRUCT;

>BARSTRUCT *bar=calloc(sizeof(BARSTRUCT),1);
>... code involving bar ...
>free(bar);
>---------------------------------------------------------------
>Is there still memory allocated somewhere for bar->x?

No. when you call malloc (and friends) you get a pointer to a block of
memory. When you pass that pointer to free() that whole block of memory
is freed. In this case you allocated memory for a struct. When you call
free that whole struct is freed. Any pointers you have to parts of that
struct are then invalid - you may no longer use their values.

However note that in the example above you have only allocated a structure
of type BARSTRUCT which has a member x which has type pointer to FOOSTRUCT.
You have *not* however allocated any memory for any FOOSTRUCT object. bar->x
is in effect an uninitialised pointer (calloc does not guarantee that it is
a null pointer) and you need to set it to point to a valid FOOSTRUCT object
before you can dereference it. To do that you might call malloc/calloc
again to allocate space for a FOOSTRUCT object. Then to free the
datastructures you would have to call free twice. Remember malloc/calloc
allocates one object and free frees one object. To release all memory so
allocated you need to call free the same number of times that you called
malloc/calloc (assuming that there were no failures, which, incidentally
you should test for, and of course realloc can do various things).

Quote:
>In other words, does this constitute a memory leak, requiring me
>to first free(bar->x) before I free(bar)?

You must only free(bar->x) if bar->x points to an object created
by malloc/calloc/realloc. (You can also pass a null pointer to free in
which case it does nothing).

--
-----------------------------------------


-----------------------------------------



Tue, 27 Apr 1999 03:00:00 GMT  
 free()'ing structs containing pointers to memory?

Quote:

>What happens to memory referenced by pointers which are part of a
>struct, when that struct is freed?
>for example if I ...
>------------------------------------------------------
>typedef struct
>{
>    char foo[27];
>} FOOSTRUCT;
>typedef struct
>{
>    char bar[27];
>    FOOSTRUCT *x;
>} BARSTRUCT;
>BARSTRUCT *bar=calloc(sizeof(BARSTRUCT),1);
>... code involving bar ...
>free(bar);
>---------------------------------------------------------------
>Is there still memory allocated somewhere for bar->x?
>In other words, does this constitute a memory leak, requiring me
>to first free(bar->x) before I free(bar)?

You are correct, it /is/ a memory leak. You will need to free bar->x (if
it was dynamically allocated) before freeing bar. Be careful, however. If
you have a statement like: bar->x = &fooitem; then freeing bar->x can
cause Bad Things to happen.

  Bob
--
  Bob           One good thing about being wrong is the joy it brings to others



Tue, 27 Apr 1999 03:00:00 GMT  
 free()'ing structs containing pointers to memory?

Quote:

> What happens to memory referenced by pointers which are part of a
> struct, when that struct is freed?

> for example if I ...
> ------------------------------------------------------
> typedef struct
> {
>         char foo[27];
> } FOOSTRUCT;

> typedef struct
> {
>         char bar[27];
>         FOOSTRUCT *x;
> } BARSTRUCT;

> BARSTRUCT *bar=calloc(sizeof(BARSTRUCT),1);
> ... code involving bar ...
> free(bar);
> ---------------------------------------------------------------
> Is there still memory allocated somewhere for bar->x?

> In other words, does this constitute a memory leak, requiring me
> to first free(bar->x) before I free(bar)?

Presumably, after you allocated the memory for the parent
structure, you then allocated memory for the member
pointers.  If so, then you should deallocate the
pointers in reverse order:  free up child memory, then
free up parent memory (or else, how could you get to the
children once the parent was freed?).

Yours,

Geoff Houck
systems hk

http://www.teleport.com/~hksys



Tue, 27 Apr 1999 03:00:00 GMT  
 free()'ing structs containing pointers to memory?

Sure, if bar->x points to an allocated memory free (bar) doesn*t free this
memory.
You have to free bar->x and after this free bar.

Robert Rossmann



Fri, 30 Apr 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. how to prevent 'free'-ing memory twice

2. Q:Frequent malloc'ing and free'ing

3. free()'ing structs .. continued

4. Free'ing memory

5. malloc()'ing struct to struct and comparison

6. memory block containing pointers, aka pointer to pointer i believe

7. Quickie on free()'ing malloc()'d mem

8. memory leak: pointer->pointer->pointer->struct

9. free()ing memory in the case of an abnormal exit

10. free-ing memory problem

11. memory Leak: pointer->pointer->pointer->struct

12. Error "free"-ing "malloc"-ed memory

 

 
Powered by phpBB® Forum Software