Delete a pointer declared in function that's set equal to onereturned from func?

R

Rob

If I have a function that has a variable declared inside it (which is
a pointer) that can be set equal to a pointer returned from another
function, do i need to delete it before returning from the function?

Code:
void test()
{
Test* t = obj.getTest();
...elided...

//do this? (assuming others need to use what's in get() )
delete t;
t = NULL;
}
 
I

Ian Collins

Rob said:
If I have a function that has a variable declared inside it (which is
a pointer) that can be set equal to a pointer returned from another
function, do i need to delete it before returning from the function?

Code:
void test()
{
Test* t = obj.getTest();
...elided...

//do this? (assuming others need to use what's in get() )
delete t;
t = NULL;
}

It all depends on what is returned. getTest() should document who owns
the returned pointer.
 
S

sk_usenet

Rob said:
If I have a function that has a variable declared inside it (which is
a pointer) that can be set equal to a pointer returned from another
function, do i need to delete it before returning from the function?

Code:
void test()
{
Test* t = obj.getTest();
...elided...

//do this? (assuming others need to use what's in get() )
delete t;
t = NULL;
}

First off, has the memory been acquired through the new operator? If no,
then this is senseless. Other issue would be of ownership. If your function
expects your callers to deallocate the memory, then yes. Btw, if new [] has
been used then delete [] should be used.
 
J

Joe Greer

If I have a function that has a variable declared inside it (which is
a pointer) that can be set equal to a pointer returned from another
function, do i need to delete it before returning from the function?

Code:
void test()
{
Test* t = obj.getTest();
...elided...

//do this? (assuming others need to use what's in get() )
delete t;
t = NULL;
}

It depends upon what getTest() is returning. If it is returning an
allocated object, then I would probably prefer:

std::auto_ptr<Test> t(obj.getTest());

and let the auto_ptr delete it for me. If you have control over getTest(),
then you could even have it return an auto_ptr<> to document that it is
returning an allocated object and that you need to handle it.

Another possibility is that the ownership of the object is given to several
different clients. In which case, you should consider shared_ptr<> (either
boost or TR1) and having getTest() return a shared_ptr<>.

On the otherhand, if getTest() is returning a pointer to an internal object
of some sort and you aren't supposed to delete it, then that is what you
do. Again, if you have any influence over getTest() then you should have
that method return a reference instead of a pointer to indicate that you
are referencing and existing object and not responsible for its lifetime.

Some good coding conventions can go along way toward having these kinds of
questions answer themselves. Sadly, one doesn't often have total control
over that kind of thing.

Hope that helps,
joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top