473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Freeing memory allocated by another function

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.
Dec 5 '07 #1
6 2013
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/

--
Er*********@sun.com
Dec 5 '07 #2
On Dec 5, 6:39 pm, Praetorian <ashish.sadanan...@gmail.comwrote:
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.

Dec 5 '07 #3
On Dec 5, 10:39 am, Praetorian <ashish.sadanan...@gmail.comwrote:
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
Dec 5 '07 #4
On Dec 5, 11:46 am, Eric Sosman <Eric.Sos...@sun.comwrote:
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.
Dec 5 '07 #5
On Dec 5, 11:56 am, Spiros Bousbouras <spi...@gmail.comwrote:
On Dec 5, 6:39 pm, Praetorian <ashish.sadanan...@gmail.comwrote:
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.
Dec 5 '07 #6
On Dec 5, 12:39 pm, fred.l.kleinschm...@boeing.com wrote:
On Dec 5, 10:39 am, Praetorian <ashish.sadanan...@gmail.comwrote:
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.
Dec 5 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hassan Iqbal | last post by:
Hi, (1)if i do the memory allocation like: int **p; p= malloc(n*sizeof p); for(i=0;i<n;i++) /* i and n are integers */ p= malloc(n*sizeof p); then in that case is this command sufficient to...
12
by: f.oppedisano | last post by:
Hi, i would like to allocate two structures making only one malloc call. So i do prt=malloc(sizeof(struct1)+sizeof(struct2)); After this operation i make two pointers one to the first struct...
29
by: keredil | last post by:
Hi, Will the memory allocated by malloc get released when program exits? I guess it will since when the program exits, the OS will free all the memory (global, stack, heap) used by this...
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
1
by: skg | last post by:
I am passing the address of pointer like char** from managed extension and getting the its initialized value from a C library dll. How can i free the memory from the code in Managed Extension ?...
2
by: tristan.chaplin | last post by:
I have an unmanaged dll that returns a pointer (that it allocates, I didn't allocate it with Marshal.AllocHGlobal), how do I free the memory when I'm finished with it? It doesn't seem to like me...
3
by: william | last post by:
My situation is here: an array of two dimension can only be defined locally within a function(because the caller don't know the exact size ). Then the question is: how should the caller access...
3
by: Mad Hatter | last post by:
Hi folks I have a script which reads the mysql database,does a lot of string manipulation then writes its output to a file. The manipulation is all done in a separate function which is called...
11
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
The output what i am expecting is a segmentation fault but some output is coming. char *str = get_time();//malloc is done in this function and returning that char* char *str1...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.