freeing structs

D

dmjcunha

Hi, if I have:

struct rec2 {
int i;
};
struct rec1 {
struct rec2 two;
};

int func1(void) {
struct rec1 *one;

one = malloc(sizeof(struct rec1));

func2(&one->two);

/* here should I "free(one)" or that would
scramble func2 or any other damage... */

return 1;
}
Thanks in advance.
 
K

Kaz Kylheku

Hi, if I have:

struct rec2 {
int i;
};
struct rec1 {
struct rec2 two;
};

int func1(void) {
struct rec1 *one;

one = malloc(sizeof(struct rec1));

func2(&one->two);

/* here should I "free(one)" or that would
scramble func2 or any other damage... */

That's impossible to say without knowing the behavior of func2.

func2 could store the pointer somewhere, and later some code could rely on that
pointer, which would be bad if the object has been freed.

If func2 uses the pointer only while it is executing, and doesn't save it
anywhere, it is safe to free it. In fact, if you don't free it, there will be
a memory leak, because other func1 loses the reference to the allocated object
when the "one" variable goes out of scope.
 
I

Ian Collins

Hi, if I have:

struct rec2 {
int i;
};
struct rec1 {
struct rec2 two;
};

int func1(void) {
struct rec1 *one;

one = malloc(sizeof(struct rec1));

func2(&one->two);

/* here should I "free(one)" or that would
scramble func2 or any other damage... */

fuct2 to has done it's work by this point, so yes you should free one
here.

I'm sure others will point out that it is cleaner and safer to write

struct rec1* one = malloc(sizeof *one);

so I'll save them the trouble.
 
E

Eric Sosman

Hi, if I have:

struct rec2 {
int i;
};
struct rec1 {
struct rec2 two;
};

int func1(void) {
struct rec1 *one;

one = malloc(sizeof(struct rec1));

Aside: This works, but in a program that uses many different
struct types it's easy to get them mixed up. Writing it as

one = malloc(sizeof *one);

.... avoids that possible problem.

Aside: malloc() can fail and return NULL. Get in the habit
of checking for that possibility before using the pointer.
func2(&one->two);

/* here should I "free(one)" or that would
scramble func2 or any other damage... */

return 1;
}

You haven't shown us what func2() does, so we can't give a
definite answer. If func() is "well-behaved," that is, if it
uses its parameters and then forgets about them when it returns,
then nothing you do with `one' after func2() returns will cause
any harm -- not on func2()'s account, anyway.

However, if func2() does something "unfriendly" like store
its parameter value in a global variable, then freeing `one' will
leave that global variable pointing to nowhere; formally, its
value will be "indeterminate." The same applies if func2() stores
its parameter value (or something based on that value) in any
other long-lived data structure, too.
 
J

Joe Pfeiffer

Hi, if I have:

struct rec2 {
int i;
};
struct rec1 {
struct rec2 two;
};

int func1(void) {
struct rec1 *one;

one = malloc(sizeof(struct rec1));

func2(&one->two);

/* here should I "free(one)" or that would
scramble func2 or any other damage... */

return 1;
}
Thanks in advance.

I don't quite know what you mean by "scramble func2". It has executed
at this point, so freeing an object you passed to it won't affect it.
If you *don't* free the object, you'll have a memory leak in your
program.

I notice that you passed func2() the address of a field in your struct
rec1. If func2() stashed away this pointer for some other code in your
program to use later (in a global, for instance), you'll be trying to
access a free'd object, which will be a bug.
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top