Using Free

S

stevenruiz

Hello Everyone,

I am unsure on how to use free() in my function. An example is
below:

main.c

int main()
{
char values[123];

while(getchar() != 'x')
{
read(0, &values, sizeof(values))
function_1(values)
}

}


test.c


char** function_1(char *values)
{
char *values[200];
char** accessValues;


//other code....
values[0] = malloc(123 * sizeof(char *));
accessValues = malloc(123 * sizeof(char *));

accessValues = values;

return accessValues;
}


Please disregard some of the naming conventions. If I am performing
operations on values variable only in the function, but I loop over
the function constantly, where do I free() memory? How would this be
accomplish assuming memory must be freed in a function as opposed to
operations only done in a main? Any help would be appreciated.
 
S

stevenruiz

Hello Everyone,

I am unsure on how to use free() in my function. An example is
below:

main.c

int main()
{
char values[123];

while(getchar() != 'x')
{
read(0, &values, sizeof(values))
function_1(values)
}

}

test.c

char** function_1(char *values)
{
char *values[200];
char** accessValues;

//other code....
values[0] = malloc(123 * sizeof(char *));
accessValues = malloc(123 * sizeof(char *));

accessValues = values;

return accessValues;

}

Please disregard some of the naming conventions. If I am performing
operations on values variable only in the function, but I loop over
the function constantly, where do I free() memory? How would this be
accomplish assuming memory must be freed in a function as opposed to
operations only done in a main? Any help would be appreciated.

My apologies for the double post. I thought I didn't submit it.
 
B

Barry Schwarz

Hello Everyone,

I am unsure on how to use free() in my function. An example is
below:

This program has all the same problems as the code you posted 20
minutes earlier.
 
T

Thomas Austad

Hi,

Hello Everyone,

I am unsure on how to use free() in my function. An example is
below:

main.c

int main()
{
char values[123];

while(getchar() != 'x')
{
read(0, &values, sizeof(values))

Hmm,.. The char** isn't stored in a variable so you have no chance of
freeing the memory allocated and "returned" from function_1().
function_1(values)
}

}


test.c


char** function_1(char *values)
{

This char pointer array is allocated on the stack so it's undefined when
function_1 return.
char *values[200];
char** accessValues;


//other code....
values[0] = malloc(123 * sizeof(char *));
accessValues = malloc(123 * sizeof(char *));

accessValues = values;

return accessValues;
}

To return a safe pointer to the 'array of char pointers' you can do
something like this:
char **function_1( ... )
{
char **accessValues;
int i;
/* allocate array of char pointer */
accessValues = malloc( sizeof(char *)*123 );
for (i = 0; i < 123; ++i)
{
/* allocate char array */
accessValues = malloc( sizeof(char)*123 );
}
return accessValues;
}

To free all the memory allocated:
int main( void )
{
char **accessValues;

/* ... */

accessValues = function_1( ... );

/* free all memory here */
for (i = 0; i < 123; ++i)
{
free( accessValues );
}
free( accessValues );

/* ... */

return 0;
}

But,..
If you only use the allocated memory within function_1() you should
really free it before returning to main().
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top