I have a function foo like
myType** foo(){
myType **array = malloc( .... );
//Do some stuff
return array;
}
Here I have malloced , but did not free it as I am returning it . Will this cause a memory leak ? Should I explicitly free it in the calling function after I use it ?
It’s a memory leak only if you don’t free the memory (regardless where).
In this case, you should
freeit after the function is called and you’re done with the pointer.But this is the C way of doing it. In C++ you’d return a smart pointer, and use
newinstead ofmalloc.