If a function returns a pointer to something .... who manages the memory?
If a function like c_str() returns a pointer, who is responsible for cleaning up the returned space at the end of the scope? The function that gave you the pointer or you?
Re: If a function returns a pointer to something .... who manages the memory?
It depends. In the case of c_str from std::string, the caller is not responsible.
Re: If a function returns a pointer to something .... who manages the memory?
Quote:
Originally Posted by
bigc++
If a function like c_str() returns a pointer, who is responsible for cleaning up the returned space at the end of the scope? The function that gave you the pointer or you?
You need to read the documentation re the specific function. In general, for a non-class function the caller is responsible and for a class function (eg c_str()) the caller is not responsible (destructor frees the memory). However, there are exceptions to this - it all depends upon the function specification! Also, in those cases when it is the callers responsibility to free the memory the documentation should specifiy how this is to be done - sometimes there are specific functions provided to free memory and these need to be used.