P
Praetorian
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(¶m1, 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.
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(¶m1, 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.