473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

confusion regarding auto pointers

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.

Aug 12 '05 #1
8 3308
gg wrote:
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/incu...oc/index.html).
auto_ptr owns object so in second case object is deleted twice.

Best
Darek

Aug 12 '05 #2
gg wrote:
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.
--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Aug 12 '05 #3
gg wrote:
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

Aug 12 '05 #4

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

Aug 12 '05 #5
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?

Aug 12 '05 #6
In message <11**********************@g49g2000cwa.googlegroups .com>, gg
<gg*****@gmail.com> writes
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 ( ) ) );

--
Richard Herring
Aug 12 '05 #7
In message <11*********************@z14g2000cwz.googlegroups. com>, gg
<gg*****@gmail.com> writes
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.

--
Richard Herring
Aug 12 '05 #8
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.

Aug 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
16
by: Rob Meade | last post by:
Hi all, I am just making a few methods in my base class, one of which returns a horizontal line, formatted on the page as *I* want it..its basically a collection of three table rows. I...
1
by: archana | last post by:
Hi all, I am having one confusion regarding changing cursor to wait cursor I am providing auto refreshing facility for listview using timer. So what i am doing is when auto refreshing is in...
4
by: archana | last post by:
Hi all, I am having one confusion regarding invoking web method of web service asychronously through windows applicaiton. What i am doing is i am having one long runing web method whose one...
3
by: archana | last post by:
Hi all, I have one confusion regarding threading in windows service which is developed in c#. What i am doing is on 'onstart' event i am starting one thread. In thread procedure i am...
20
by: sam_cit | last post by:
Hi Everyone, I just heard from a friend of mine that there are few c compilers that give an error when pointers are not initialised to NULL. Is it correct? and if so, is there any standard for...
8
by: Erwin Moller | last post by:
Hi group, I could use a bit of guidance on the following matter. I am starting a new project now and must make some decisions regarding encoding. Environment: PHP4.3, Postgres7.4.3 I must...
2
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that...
40
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.