Hello!
My function e.g. foo returns the malloc'ed value (pointer).
int *foo(int size){
int *tmp;
tmp=malloc(sizeof(int)*size);
if(tmp != NULL) {
tmp[2]=5;
}
return tmp;
}
Where should the allocated memory be freed? In the function, where I'm calling it?
main(){
int *ar1, size=10;
ar1 = foo(size);
if(ar1 != NULL) {
ar1[1]=6;
}
free (ar1);
}
Is that OK? My doubts are: I actually didn't allocate ar1. And according to my C/C++ Reference Guide "You can only free () a pointer, which is allocated by malloc/calloc etc."
Could someone explain to me how does it work?
And the second question:
Could I somehow prove whether the memory has been freed?
Is there any function in C for this?
Thank you very much.
My function e.g. foo returns the malloc'ed value (pointer).
int *foo(int size){
int *tmp;
tmp=malloc(sizeof(int)*size);
if(tmp != NULL) {
tmp[2]=5;
}
return tmp;
}
Where should the allocated memory be freed? In the function, where I'm calling it?
main(){
int *ar1, size=10;
ar1 = foo(size);
if(ar1 != NULL) {
ar1[1]=6;
}
free (ar1);
}
Is that OK? My doubts are: I actually didn't allocate ar1. And according to my C/C++ Reference Guide "You can only free () a pointer, which is allocated by malloc/calloc etc."
Could someone explain to me how does it work?
And the second question:
Could I somehow prove whether the memory has been freed?
Is there any function in C for this?
Thank you very much.