It does not matter if you allocate in a called function or within the code that would call the function. In both cases the code (more precise: the thread) that triggers the allocation owns the memory and is responsible for freeing it.
Two additional notes:
There is no need to check for a
NULL
pointer when calling
delete
. The implementation of
delete
will do that too and just does nothing when the pointer is
NULL
.
When deleting an array you should use the
delete[]
operator:
delete[] data;
You may have also a look at C++ smart pointers:
auto_ptr - C++ Reference[
^]
shared_ptr - C++ Reference[
^]
unique_ptr - C++ Reference[
^]
weak_ptr - C++ Reference[
^]