Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Freeing a pointer (http://www.go4expert.com/forums/freeing-a-pointer-t11268/)

Plfgoa 11Jun2008 17:19

Freeing a pointer
 
Hi,

If a function is returning a pointer and if that pointer was returned by a malloc function , how do I free that pointer ? If I run valgrind , it will show memory leak since "k" was not freed. How do I free "k" in following ?

Example:

int * sum(int a, int b)
{
int sum;
int *k=malloc(sizeof(int));
sum=a+b;
k=∑
return k;
}

Thank you.

-PLF

gunshot08 11Jun2008 19:38

Re: Freeing a pointer
 
you use the free() function

int * sum(int a, int b)
{
int sum;
int *k=malloc(sizeof(int));
sum=a+b;
k=∑
return k;
free(k);
}

Plfgoa 11Jun2008 20:10

Re: Freeing a pointer
 
Hi,

Thanks for the response . Just wanted to know if one uses free() after a return statement will free() ever get called ?

Thanks.

-PLF


All times are GMT +5.5. The time now is 16:06.