Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Freeing memory allocated by another function

Reply
Thread Tools

Freeing memory allocated by another function

 
 
Praetorian
Guest
Posts: n/a
 
      12-05-2007
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 FuncB looks at the other parameters and decides whether or not to
allocated memory to the int * passed to it (using calloc()). One of
the other parameters passed is a structure and if FuncB does allocate
memory it'll point one of the members of this structure to the int *
parameter.

void FuncA(.....)
{
struct param1;
int * param2;

FuncB(&param1, param2);
<do something>
if(param2!=NULL) {
free(param2);
}
}

void FuncB(struct *param1, int *param2)
{
if(<some conditions>) {
param2 = calloc(<some memory>);
param1->memberVar = param2;
}
}

Is this valid? Can the memory allocated within another function be
freed by a calling function as long as the original pointer variable
stays the same?

Now the second part:
I implemented the code above but it's showing me some strange results
when I breakpoint and watch the different variables in Visual Studio
2005.

Here's what I see:
Within FuncA -
param2 is NULL;
Within FuncB -
conditions for allocating memory evaluate to true.
memory is allocated and assigned to param2 (I'm not using calloc()
directly but through a library wrapper function that throws an error
if the allocation fails)
but param2 is still NULL (0x00000000) in the watch window.
however, when the line "param1->memberVar = param2;" executes it
(memberVar) has a valid address (it was previously NULL too).
I can even assign values using "param1->memberVar[0] = 1;" etc.
Back outside in FuncA
structure param1 is used by some other library functions to do
stuff. The values in memberVar are all valid and correct but param2 is
still NULL!
As a result FuncA cannot free the memory allocated to param2.

What's going wrong here?

Thanks in advance,
Ashish.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-05-2007
Praetorian wrote:
> This is actually 2 questions:
> [...]


I think it's really just one question. Specifically,
it's Question 4.8 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.c-faq.com/

--

 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 6:39 pm, Praetorian <ashish.sadanan...@gmail.com> wrote:
> 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 FuncB looks at the other parameters and decides whether or not to
> allocated memory to the int * passed to it (using calloc()). One of
> the other parameters passed is a structure and if FuncB does allocate
> memory it'll point one of the members of this structure to the int *
> parameter.
>
> void FuncA(.....)
> {
> struct param1;


I'm not sure if the line above is legal but it
certainly doesn't do anything useful.

> int * param2;
>
> FuncB(&param1, param2);
> <do something>
> if(param2!=NULL) {
> free(param2);
> }
>
> }
>
> void FuncB(struct *param1, int *param2)


So param1 is a pointer to some structure but we
don't know which one ? Not legal I'm afraid.

> {
> if(<some conditions>) {
> param2 = calloc(<some memory>);
> param1->memberVar = param2;
> }
>
> }
>
> Is this valid? Can the memory allocated within another function be
> freed by a calling function as long as the original pointer variable
> stays the same?


Yes it can be freed. It is not an issue of a variable
staying the same but of calling free() with a value
returned by calloc() , malloc() etc.

> Now the second part:
> I implemented the code above but it's showing me some strange results
> when I breakpoint and watch the different variables in Visual Studio
> 2005.
>
> Here's what I see:
> Within FuncA -
> param2 is NULL;
> Within FuncB -
> conditions for allocating memory evaluate to true.
> memory is allocated and assigned to param2 (I'm not using calloc()
> directly but through a library wrapper function that throws an error
> if the allocation fails)
> but param2 is still NULL (0x00000000) in the watch window.
> however, when the line "param1->memberVar = param2;" executes it
> (memberVar) has a valid address (it was previously NULL too).
> I can even assign values using "param1->memberVar[0] = 1;" etc.
> Back outside in FuncA
> structure param1 is used by some other library functions to do
> stuff. The values in memberVar are all valid and correct but param2 is
> still NULL!
> As a result FuncA cannot free the memory allocated to param2.
>
> What's going wrong here?


You have not posted compilable code. You have not
even posted the declaration for the structure param1.
However based on what you posted I see no reason why
the assignment "param2 = calloc(<some memory>);" in
FuncB should modify param2 in FuncA. If you want param2
in FuncA to be modified then you need to pass a pointer
to param2 when you call FuncB , not param2 itself.

 
Reply With Quote
 
fred.l.kleinschmidt@boeing.com
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 10:39 am, Praetorian <ashish.sadanan...@gmail.com> wrote:
> 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 FuncB looks at the other parameters and decides whether or not to
> allocated memory to the int * passed to it (using calloc()). One of
> the other parameters passed is a structure and if FuncB does allocate
> memory it'll point one of the members of this structure to the int *
> parameter.
>
> void FuncA(.....)
> {
> struct param1;
> int * param2;
>
> FuncB(&param1, param2);
> <do something>
> if(param2!=NULL) {
> free(param2);
> }
>
> }
>
> void FuncB(struct *param1, int *param2)
> {
> if(<some conditions>) {
> param2 = calloc(<some memory>);
> param1->memberVar = param2;
> }
>
> }
>
> Is this valid?

<snip>

No, this is not correct. You need something like this:

void FuncA(.....)
{
struct somekind param1;
int *param2;

/* Initialize param2 in case FuncB doesn't */
param2 = NULL;

FuncB(&param1, &param2);
<do something>
if(param2!=NULL) {
free(param2);
}
}


void FuncB(struct sometype *param1, int **param2)
{
if(<some conditions>) {
*param2 = calloc(<some memory>);
param1->memberVar = *param2;
}
}

However, since you assign the allocation to param1->memberVar
(which I assume is an int*), it makes more sense to do this:

void FuncA(.....)
{
struct somekind param1;

FuncB(&param1);
<do something>
if(param1.memberVar!=NULL) {
free(param1.memberVar);
param1.memberVar=NULL;
}
}

void FuncB(struct sometype *param1)
{
if(<some conditions>) {
/* before doing this, you should worry
* about whether memberVar already points somewhere
*/
param1->memberVar = calloc(<some memory>);
}
else {
param1->memberVar = NULL;
}
}

--
Fred Kleinschmidt
 
Reply With Quote
 
Praetorian
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 11:46 am, Eric Sosman <Eric.Sos...@sun.com> wrote:
> Praetorian wrote:
> > This is actually 2 questions:
> > [...]

>
> I think it's really just one question. Specifically,
> it's Question 4.8 in the comp.lang.c Frequently Asked
> Questions (FAQ) list
>
> http://www.c-faq.com/
>
> --
> Eric.Sos...@sun.com


Exactly what I needed, thanks!

Ashish.
 
Reply With Quote
 
Praetorian
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 11:56 am, Spiros Bousbouras <spi...@gmail.com> wrote:
> On Dec 5, 6:39 pm, Praetorian <ashish.sadanan...@gmail.com> wrote:
>
> > 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 FuncB looks at the other parameters and decides whether or not to
> > allocated memory to the int * passed to it (using calloc()). One of
> > the other parameters passed is a structure and if FuncB does allocate
> > memory it'll point one of the members of this structure to the int *
> > parameter.

>
> > void FuncA(.....)
> > {
> > struct param1;

>
> I'm not sure if the line above is legal but it
> certainly doesn't do anything useful.
>
> > int * param2;

>
> > FuncB(&param1, param2);
> > <do something>
> > if(param2!=NULL) {
> > free(param2);
> > }

>
> > }

>
> > void FuncB(struct *param1, int *param2)

>
> So param1 is a pointer to some structure but we
> don't know which one ? Not legal I'm afraid.
>
> > {
> > if(<some conditions>) {
> > param2 = calloc(<some memory>);
> > param1->memberVar = param2;
> > }

>
> > }

>
> > Is this valid? Can the memory allocated within another function be
> > freed by a calling function as long as the original pointer variable
> > stays the same?

>
> Yes it can be freed. It is not an issue of a variable
> staying the same but of calling free() with a value
> returned by calloc() , malloc() etc.
>
>
>
> > Now the second part:
> > I implemented the code above but it's showing me some strange results
> > when I breakpoint and watch the different variables in Visual Studio
> > 2005.

>
> > Here's what I see:
> > Within FuncA -
> > param2 is NULL;
> > Within FuncB -
> > conditions for allocating memory evaluate to true.
> > memory is allocated and assigned to param2 (I'm not using calloc()
> > directly but through a library wrapper function that throws an error
> > if the allocation fails)
> > but param2 is still NULL (0x00000000) in the watch window.
> > however, when the line "param1->memberVar = param2;" executes it
> > (memberVar) has a valid address (it was previously NULL too).
> > I can even assign values using "param1->memberVar[0] = 1;" etc.
> > Back outside in FuncA
> > structure param1 is used by some other library functions to do
> > stuff. The values in memberVar are all valid and correct but param2 is
> > still NULL!
> > As a result FuncA cannot free the memory allocated to param2.

>
> > What's going wrong here?

>
> You have not posted compilable code. You have not
> even posted the declaration for the structure param1.
> However based on what you posted I see no reason why
> the assignment "param2 = calloc(<some memory>);" in
> FuncB should modify param2 in FuncA. If you want param2
> in FuncA to be modified then you need to pass a pointer
> to param2 when you call FuncB , not param2 itself.


Spiros,
Sorry about the code I posted, I knew it was syntactically incorrect
but it was just meant to give you an idea of my problem. The problem
is indeed that I'm passing the pointer itself instead of a reference
to it. Thanks for your help.

Ashish.
 
Reply With Quote
 
Praetorian
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 12:39 pm, fred.l.kleinschm...@boeing.com wrote:
> On Dec 5, 10:39 am, Praetorian <ashish.sadanan...@gmail.com> wrote:
>
> > 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 FuncB looks at the other parameters and decides whether or not to
> > allocated memory to the int * passed to it (using calloc()). One of
> > the other parameters passed is a structure and if FuncB does allocate
> > memory it'll point one of the members of this structure to the int *
> > parameter.

>
> > void FuncA(.....)
> > {
> > struct param1;
> > int * param2;

>
> > FuncB(&param1, param2);
> > <do something>
> > if(param2!=NULL) {
> > free(param2);
> > }

>
> > }

>
> > void FuncB(struct *param1, int *param2)
> > {
> > if(<some conditions>) {
> > param2 = calloc(<some memory>);
> > param1->memberVar = param2;
> > }

>
> > }

>
> > Is this valid?

>
> <snip>
>
> No, this is not correct. You need something like this:
>
> void FuncA(.....)
> {
> struct somekind param1;
> int *param2;
>
> /* Initialize param2 in case FuncB doesn't */
> param2 = NULL;
>
> FuncB(&param1, &param2);
> <do something>
> if(param2!=NULL) {
> free(param2);
> }
>
> }
>
> void FuncB(struct sometype *param1, int **param2)
> {
> if(<some conditions>) {
> *param2 = calloc(<some memory>);
> param1->memberVar = *param2;
> }
>
> }
>
> However, since you assign the allocation to param1->memberVar
> (which I assume is an int*), it makes more sense to do this:
>
> void FuncA(.....)
> {
> struct somekind param1;
>
> FuncB(&param1);
> <do something>
> if(param1.memberVar!=NULL) {
> free(param1.memberVar);
> param1.memberVar=NULL;
> }
>
> }
>
> void FuncB(struct sometype *param1)
> {
> if(<some conditions>) {
> /* before doing this, you should worry
> * about whether memberVar already points somewhere
> */
> param1->memberVar = calloc(<some memory>);
> }
> else {
> param1->memberVar = NULL;
> }
>
> }
>
> --
> Fred Kleinschmidt


Fred,
param1->memberVar is an int *; and what you've said is absolutely
correct; in fact that was the way I wanted to implement it at first.
However, the members of param1 are initialized by FuncA by several
calls to library functions and so memberVar may already be non-NULL.
Also, in the actual program FuncB take 2 structure pointer parameters
and 2 int* parameters; it then attempts to cross fill any missing
information in both structures using a set of rules. So if I was to
implement it the way you suggested FuncA would need to remember
whether the memberVar pointers in the 2 structures were initialized
before calling FuncB. I figured this was a better way to do it since
FuncB could do all that by itself.

Ashish.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
freeing allocated memory Curley Q. C Programming 7 04-30-2004 04:09 PM
freeing allocated memory binaya C Programming 11 10-19-2003 06:44 PM
some queries on freeing memory allocated using malloc Hassan Iqbal C Programming 3 09-25-2003 02:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57