HI,
I am not sure whether this code is legal... Its works fine, but I am afraid that this is not a valid thing to be done. I am freeing the returned dynamically allocated memory in the main function. In the following code below, Is the freeing of memory in either of the functions is the same, or else there is a memory leak?.
[code=c]
char * foo(void){
char *a = (char*)malloc(s izeof(char)*2);
return a;
}
int main(){
char *p = foo();
//do some thing with 'p' here....
free p;
return 0;
}
[/code]
Thanks,
Gsi.
I am not sure whether this code is legal... Its works fine, but I am afraid that this is not a valid thing to be done. I am freeing the returned dynamically allocated memory in the main function. In the following code below, Is the freeing of memory in either of the functions is the same, or else there is a memory leak?.
[code=c]
char * foo(void){
char *a = (char*)malloc(s izeof(char)*2);
return a;
}
int main(){
char *p = foo();
//do some thing with 'p' here....
free p;
return 0;
}
[/code]
Thanks,
Gsi.
Comment