if I have a function that returns a pointer generated by calloc does it delete the one generated by the function when I delete the variable assigned to it.
1 2 3 4 5 6 7 8
int * somefunction() {
int * array;
array = calloc(3,sizeof(int));
return array;
}
int * bob = somefunction();
free(bob);
in other words, do I have a memory leak here? Sorry if I miss something in the code other than the memory question; I am just typing this and not copying it in.
thank you. to add to that is there a memory leak in this implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int * somefunction() {
int * array;
array = calloc(3,sizeof(int));
return array;
}
class someclass {
int array[3] = somefunction();
int sum()= array[0]+array[1]+array[2];
}
int main(){
class someclasscall;
int someint = someclasscall.sum();
}
any leaks in this one. (I know sum wouldn't actually work but this is just a memory question)
yes. for every calloc() there has to be a free().
note that not only line 9, but also line 8 is wrong. You can't assign to members in class declaration, you can't assign pointers to arrays. if array was instead int*, then you could add a free in the destructor and fix that leak.