I am writing a program for portable devices hence memory management is a big constraint.
To my knowledge only if u free the dynamically allocated memory it can be used further..
My doubt is
I have a function which is returing a pointer.
pointer is dynamically allocated .....if I free the pointer before returning then it is useless or dangling pointer problem arises....or if I free after returning the ptr the control is lost.........
So when to free the memory......... ...
eg
:
char* example (int )
{
char *filepath,*file name;
filepath=(char* )malloc(100));
return filepath; ///Where to free the filepath memory
}
To my knowledge only if u free the dynamically allocated memory it can be used further..
My doubt is
I have a function which is returing a pointer.
pointer is dynamically allocated .....if I free the pointer before returning then it is useless or dangling pointer problem arises....or if I free after returning the ptr the control is lost.........
So when to free the memory......... ...
eg
:
char* example (int )
{
char *filepath,*file name;
filepath=(char* )malloc(100));
return filepath; ///Where to free the filepath memory
}
Comment