Using Free() from a function

S

stevenruiz

Hello All,

I have a question on using free in my function. I have an int
main() which calls a function and returns a char **. The problem is
I don't know when to call free after/before the function? The code is
below:

main.c

int main()
{
char inputArray[ buffer_size_main ];
char newInput [ inputBuffer ];



do
{
//End Input loop
if(newInput[0] == 'x')
break;


//Code

read(fd, &inputArray, sizeof(inputArray));
func_1 (inputArray);

write(1, "\nType return to keep tokenizing or enter 'x' to
abort", 53);

}while( read(fd,&newInput,sizeof(newInput)));

// Should I free memory here>?

return 0;

}


test.c --------------



char** mytoc(char *inputData)

{
// Initialize the pointers and variables

char *pWords[buffer_size];

char **addr;
char *pChars = 0;

// Get space for storing and traversing word
pWords = malloc(buffer_size * sizeof(char *));

pChars = malloc((buffer_size) * sizeof(char));

// Look through all input

while( *inputData )

{
// other operations....

// Grabbing memory for the words
pChars = malloc((buffer_size) * sizeof(char));
pWords = malloc(buffer_size * sizeof(char *));

inputData++;

}

pStoreChars = malloc((buffer_size) * sizeof(char));


//Setup wordLocatorAddr for return and free memory
addr = pWords;

free(pStoreChars);


return addr;
}
 
S

stevenruiz

Hello All,

I have a question on using free in my function. I have an int
main() which calls a function and returns a char **. The problem is
I don't know when to call free after/before the function? The code is
below:

main.c

int main()
{
char inputArray[ buffer_size_main ];
char newInput [ inputBuffer ];

do
{
//End Input loop
if(newInput[0] == 'x')
break;

//Code

read(fd, &inputArray, sizeof(inputArray));
func_1 (inputArray);

write(1, "\nType return to keep tokenizing or enter 'x' to
abort", 53);

}while( read(fd,&newInput,sizeof(newInput)));

// Should I free memory here>?

return 0;

}

test.c --------------

char** mytoc(char *inputData)

{
// Initialize the pointers and variables

char *pWords[buffer_size];

char **addr;
char *pChars = 0;

// Get space for storing and traversing word
pWords = malloc(buffer_size * sizeof(char *));

pChars = malloc((buffer_size) * sizeof(char));

// Look through all input

while( *inputData )

{
// other operations....

// Grabbing memory for the words
pChars = malloc((buffer_size) * sizeof(char));
pWords = malloc(buffer_size * sizeof(char *));

inputData++;

}

pStoreChars = malloc((buffer_size) * sizeof(char));

//Setup wordLocatorAddr for return and free memory
addr = pWords;

free(pStoreChars);

return addr;

}


The dilemma I am experiencing is how to free the memory for the char
**pWords variable that I am using. But if you look at my main.c I am
performing a loop. Therefore, I don't know when I will be done.
Would I free the pWords right after the function is executed in the
main.c or after the whole do/while loop before return 0. Any help
would be appreciated.
 
C

CBFalconer

I have a question on using free in my function. I have an int
main() which calls a function and returns a char **. The problem
is I don't know when to call free after/before the function? The
code is below: ...

The only memory you ever free is memory allocated by malloc (or the
equivalent via calloc). Then you call free with the pointer
returned originally by malloc.
 
B

Barry Schwarz

Hello All,

I have a question on using free in my function. I have an int
main() which calls a function and returns a char **. The problem is
I don't know when to call free after/before the function? The code is
below:

main.c

int main()
{
char inputArray[ buffer_size_main ];
char newInput [ inputBuffer ];



do
{
//End Input loop
if(newInput[0] == 'x')
break;


//Code

read(fd, &inputArray, sizeof(inputArray));
func_1 (inputArray);

write(1, "\nType return to keep tokenizing or enter 'x' to
abort", 53);

}while( read(fd,&newInput,sizeof(newInput)));

// Should I free memory here>?

We need to see how you call mytoc.
return 0;

}


test.c --------------



char** mytoc(char *inputData)

{
// Initialize the pointers and variables

char *pWords[buffer_size];

pWords is an automatic array. It is created when you enter mytoc.
char **addr;
char *pChars = 0;

// Get space for storing and traversing word
pWords = malloc(buffer_size * sizeof(char *));


Where is i defined and assigned a value? How will the calling program
know how many elements of pWords have been assigned values? What is
the point of this when you assign a new address to pWords in the
while loop?
pChars = malloc((buffer_size) * sizeof(char));

What is the point of this when you assign a new address to pChars in
the while loop?
// Look through all input

while( *inputData )

{
// other operations....

// Grabbing memory for the words
pChars = malloc((buffer_size) * sizeof(char));

Do you ever use the memory pChars points to?
pWords = malloc(buffer_size * sizeof(char *));

inputData++;

}

pStoreChars = malloc((buffer_size) * sizeof(char));


What is pStoreChars?
//Setup wordLocatorAddr for return and free memory
addr = pWords;

free(pStoreChars);

Who is supposed to free pChars? As it stands now you have numerous
memory leaks.
return addr;

And pWords is destroyed when you exit mytoc. Unfortunately, since
addr points to pWords, its value becomes indeterminate at this point
and can not be used by any subsequent code. If you made pWords static
it would remain in existence throughout your program but would
probably not support recursive calls to mytoc.
 
B

Ben Bacarisse

Hello All,

I have a question on using free in my function. I have an int
main() which calls a function and returns a char **. The problem is
I don't know when to call free after/before the function? The code is
below:

main.c

int main()
{
char inputArray[ buffer_size_main ];
char newInput [ inputBuffer ];

do
{
//End Input loop
if(newInput[0] == 'x')
break;

//Code

read(fd, &inputArray, sizeof(inputArray));
func_1 (inputArray);

write(1, "\nType return to keep tokenizing or enter 'x' to
abort", 53);

}while( read(fd,&newInput,sizeof(newInput)));

// Should I free memory here>?

return 0;

}

test.c --------------

char** mytoc(char *inputData)

{
// Initialize the pointers and variables

char *pWords[buffer_size];

char **addr;
char *pChars = 0;

// Get space for storing and traversing word
pWords = malloc(buffer_size * sizeof(char *));

pChars = malloc((buffer_size) * sizeof(char));

// Look through all input

while( *inputData )

{
// other operations....

// Grabbing memory for the words
pChars = malloc((buffer_size) * sizeof(char));
pWords = malloc(buffer_size * sizeof(char *));

inputData++;

}

pStoreChars = malloc((buffer_size) * sizeof(char));

//Setup wordLocatorAddr for return and free memory
addr = pWords;

free(pStoreChars);

return addr;

}


The dilemma I am experiencing is how to free the memory for the char
**pWords variable that I am using.


What is memory "for" a variable? pWords is an array, local to mytoc
(C calls this an automatic variable), and you return a pointer to this
local variable. This is never correct because that object only exists
with the function is executing.

But this is not really the problem. The program is full of oddities
and errors like using undefined variables, allocating memory and
overwriting the returned pointers, freeing allocated memory without
using it and having a main function that does not use the mytoc
function. I think you've got some fragments of code from various
places are and trying to squeeze them into a program.
But if you look at my main.c I am
performing a loop. Therefore, I don't know when I will be done.
Would I free the pWords right after the function is executed in the
main.c or after the whole do/while loop before return 0.

This is not the right question to be asking. pWords should never be
freed since it is not allocated using malloc (or friends). A better
question might be "I need to write a program to do XXXX. How should I
go about organising it?".
 

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,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top