confusion regarding auto pointers

G

gg

I am confused regarding what the line in the following function does.
It seems to work ok. It seems to be creating a new T object using the
pointer to an existing T object. But which function is it calling to
create the new T object?

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );
}

If I do this,

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( aPT.get ( ) );
}

Then I get core dump.
 
J

Joe Seigh

gg said:
I am confused regarding what the line in the following function does.
It seems to work ok. It seems to be creating a new T object using the
pointer to an existing T object. But which function is it calling to
create the new T object?

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );
}
The constructor for the new T is copying the input object, i.e.
initializing the new object using the input object, aPT.get() value.
If I do this,

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( aPT.get ( ) );
}

Then I get core dump.
The dtor for newPT is deleting the input object which is still
being used apparently by the caller. You should only use
auto_ptr for objects created in the same scope as the auto_ptr.
 
J

Josh Mcfarlane

gg said:
I am confused regarding what the line in the following function does.
It seems to work ok. It seems to be creating a new T object using the
pointer to an existing T object. But which function is it calling to
create the new T object?

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );
}

If I do this,

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( aPT.get ( ) );
}

Then I get core dump.

If you want to assume control of aPT in the second function (which
would get deleted as soon as f ends) you would want to have:

auto_ptr<T> newPT (aPT);

This would take the aPT pointer, transfer ownership of it to newPT, and
then delete it when f returned.

Josh McFarlane
 
V

void

gg said:
I am confused regarding what the line in the following function does.
It seems to work ok. It seems to be creating a new T object using the
pointer to an existing T object. But which function is it calling to
create the new T object?

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );
}

If I do this,

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( aPT.get ( ) );
}

Then I get core dump.
Read manual for auto_ptr class(e.g.:
http://svn.apache.org/repos/asf/incubator/stdcxx/trunk/doc/index.html).
auto_ptr owns object so in second case object is deleted twice.

Best
Darek
 
M

msalters

Joe Seigh schreef:
The dtor for newPT is deleting the input object which is still
being used apparently by the caller. You should only use
auto_ptr for objects created in the same scope as the auto_ptr.

There is no reason for that. In fact, a common use for auto_ptr<>
is to implement source and sink interfaces. A 'source' is a function
returning a std::auto_ptr<T>. If a caller doesn't save the return
value, the T object returned is deleted immediately. However, the
caller can copy the returned auto_ptr in its own auto_ptr. That
transfers ownership to another auto_ptr, so we again know the T will
be deleted.
Sinks work the other way around. A function that has a std::auto_ptr<T>
interface will take care of destroying that T, automatically.

Of course, calling sink(source()); is legal and sensible. It transfers
ownership from source() to sink() without difficult code or risks.
Even exceptions can't interfere. It's really hard to "lose" objects
that
aren't on the heap.

HTH,
Michiel Salters
 
G

gg

My main confusion is that in the line,

auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );

aPT.get () returns a T *.

so when you say,

new T ( T * );

which constructor/method gets invoked?
 
R

Richard Herring

gg said:
I am confused regarding what the line in the following function does.
It seems to work ok. It seems to be creating a new T object using the
pointer to an existing T object. But which function is it calling to
create the new T object?

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );

Are you sure that line isn't
auto_ptr < T > newPT ( new T ( * aPT.get ( ) ) );

(which creates a new T, and calls its copy-constructor to initialise it
from the one aPT points to, then initialises newPT with a pointer to the
new object) ?

If you really do mean the first form, then T must have a constructor
which takes a pointer to (const?) T as argument: T::T(T *) or T::T(T
const *).
}

If I do this,

void f ( auto_ptr < T > & aPT )
{
auto_ptr < T > newPT ( aPT.get ( ) );
}
Now you have two auto_ptrs pointing at one object. It gets deleted as
soon as newPT goes out of scope, but the pointer passed in to f() still
believes it owns the (now deleted) object, so from there on any access
is undefined behaviour..
Then I get core dump.
Not surprising.
 
R

Richard Herring

gg said:
My main confusion is that in the line,

auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );

aPT.get () returns a T *.

so when you say,

new T ( T * );

which constructor/method gets invoked?
T::T(T*) or T::T(T const *)

or it's a typo and you meant

auto_ptr < T > newPT ( new T ( * aPT.get ( ) ) );
 
G

gg

There are no T::T(T*) or T::T(T const *) methods declared in the
class. Also, the copy constructor and assignment operator are declared
private for T.

so this doesn't compile -

auto_ptr < T > newPT ( new T ( * aPT.get ( ) ) );

That is why I tried,

auto_ptr < T > newPT ( new T ( aPT.get ( ) ) );

And it worked. But I don't know why.
 

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,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top