[LC++]STL and auto_ptr woes

George T. Talbot george at phat.com
Thu Mar 7 07:03:05 UTC 2002


OK...but be warned that you'll have corrupt auto_ptr<> structures in your
collection, as the STL and auto_ptr<> aren't compatible.

You can prove this to yourself--experiment with putting some printf()'s
(cout's ;^) in your constructors and destructors.  With the appropriate test
program, you'll see the destructors for the objects held by auto_ptr<> freed
before you may have realized.

--George


----- Original Message -----
From: "Charles Randle" <charlesrandle at hotmail.com>
To: <tuxcpprogramming at lists.linux.org.au>
Sent: Wednesday, March 06, 2002 1:44 PM
Subject: Re: [LC++]STL and auto_ptr woes


> Thanks for the info , I tried the suggestion it appears to have removed
> error.
> Once again , THANKS !
>
> Best Regards,
> Charles Randle
>
>
> >From: "George T. Talbot" <george at phat.com>
> >To: <tuxcpprogramming at lists.linux.org.au>, <charlesrandle at hotmail.com>
> >Subject: Re: [LC++]STL and auto_ptr woes
> >Date: Wed, 6 Mar 2002 09:39:48 -0500
> >
> >I don't think that auto_ptr<> supports the assignment operator--it only
> >supports initialization via the constructor.  That's why it won't work in
> >your scenario.
> >
> >Change that line to read like this:
> >
> >::std::auto_ptr<someobject1> vb(BigMap2["located"].second.first);
> >
> >That should get rid of the compile error.  However, this will fail
> >horribly.
> >When vb goes out of scope, the object will be freed.  Then you'll have a
> >dangling pointer in your list and your program will crash later when that
> >item in the list is accessed or freed.
> >
> >Is auto_ptr<> supposed to work in a Standard C++ Library collection?  I
> >seem
> >to recall this as being a no-no, but I don't have the book in front of me
> >right now.
> >
> >--George
> >
> >P.S.  The list seems to be operating pretty slow.  I just received this
at
> >about 9:35am EST on the 6th, and it was sent on the 4th and 2:45pm.
> >
> >
> >
> >----- Original Message -----
> >From: "Charles Randle" <charlesrandle at hotmail.com>
> >To: <tuxcpprogramming at lists.linux.org.au>
> >Sent: Monday, March 04, 2002 2:45 PM
> >Subject: [LC++]STL and auto_ptr woes
> >
> >
> > > Hi All ,
> > >
> > > I am using the STL to store pointers to objects which are expensive to
> >copy
> > > (hence the pointers.
> > >
> > > I know it isn't a good idea to use the auto_ptr class with STL classes
> >but
> >I
> > > don't readily remember why could anyone tell me why the following
> >wouldn't
> > > be a good idea ?
> > >
> > > ::std::map <
> > > ::std::string,::std::pair<auto_ptr<someobject1>,auto_ptr<someobject2>
>
> > >
> > > bigMap;
> > >
> > > Given that the above isn't a good idea I have the following:
> > >
> > > ::std::map < ::std::string , ::std::pair<someobject1 *,someobject2 *>
>
> > > BigMap2
> > >
> > > However when I assign the pointers to an auto_ptr object as in:
> > >
> > > ::std::auto_ptr<someobject1> vb = BigMap2["located"].second.first;
> > >
> > > the compiler gives the following error:
> > >
> > > simple.cpp:52:conversion from 'someobject1*' to non-scalar type
> > > 'std::auto_ptr<someobject1>' requested
> > >
> > >
> > > What is the reason for this error and what can I do to fix it ???
> > >
> > >
> > > Best Regards,
> > > Charles Randle
> > >
> > > _________________________________________________________________
> > > Chat with friends online, try MSN Messenger: http://messenger.msn.com
> > >
> > >
> > > _______________________________________________
> > > This is the Linux C++ Programming List
> > > : http://lists.linux.org.au/listinfo/tuxcpprogramming List
> > >
> >
>
>
>
>
> _________________________________________________________________
> MSN Photos is the easiest way to share and print your photos:
> http://photos.msn.com/support/worldwide.aspx
>
>
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
>





More information about the tuxCPProgramming mailing list