A function that returns an allocated pointer means that ownership of that memory is being transferred to the caller of that function. It is up to the code that receives the returned pointer to deallocate the memory.
While using malloc()
and free()
in C++ is not entirely without precedent, it should generally be avoided. You could avoid the problem entirely by changing your code to use std::vector<int>
instead of an int *
.
7
solved How can i free memory in C++/C? When would i write free(a);? Function is returing pointer