Topic: auto_ptrs and invalid pointers
Author: "Keith Lancaster" <keithl@futuresoft.com>
Date: 1997/05/29 Raw View
My apologies in advance if this is the incorrect forum...
Given that the new form of auto_ptr does not set its pointer to null when
it loses ownership, how can a user of determine if the pointer is valid?
example:
auto_ptr<MyClass> pmyClass(new myClass);
if(true)
{
// transfer ownership to pTemp, which then deletes the pointer
// when it loses scope
auto_ptr<MyClass> pTemp = pmyClass;
}
MyClass *pNoGood = pMyClass.get(); // pNoGood != 0, but points to deleted
object!
Is there a way to test ownership? Is some sort of "IsPointerValid" function
required? I'm using the implementation that comes with MS Visual C v5.0.
Thanks in advance,
Keith
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Paul D. DeRocco" <pderocco@ix::netcom::com>
Date: 1997/05/30 Raw View
Keith Lancaster wrote:
>
> Given that the new form of auto_ptr does not set its pointer to null when
> it loses ownership, how can a user of determine if the pointer is valid?
>
> example:
> auto_ptr<MyClass> pmyClass(new myClass);
> if(true)
> {
> // transfer ownership to pTemp, which then deletes the pointer
> // when it loses scope
> auto_ptr<MyClass> pTemp = pmyClass;
> }
> MyClass *pNoGood = pMyClass.get(); // pNoGood != 0, but points to deleted
> object!
>
> Is there a way to test ownership? Is some sort of "IsPointerValid" function
> required? I'm using the implementation that comes with MS Visual C v5.0.
Nope. You have to make sure you don't do that sort of thing with
auto_ptr. It's not idiot-proof.
In fact, I prefer an even more idiot-sensitive version that doesn't even
do ownership bookkeeping, which I just call ptr<T>. I find it is usually
what I want, and doesn't have the unnecessary overhead of auto_ptr.
--
Ciao,
Paul
(Please remove the extra dots from the return address,
which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]