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;
}
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;
}