Re: naked pointer vs boost::shared_ptr<T>
On Feb 28, 12:47 pm, Al <t...@haik.us> wrote:
Sushrut Sardeshmukh wrote:
Should we stop using naked pointer and replace all of them with
boost:shared_ptr or ( shared_array<T> or scoped_ptr) ?
I am trying to come up with C to C++ conversion guidelines for a large
transformation project. Your inputs will be very valuable.
If you're going to use one of the typical ones, why not std::auto_ptr?
shared_ptr has semantics which are often inconvenient / incorrect.
As does auto_ptr, or any other smart pointer. In new code, I
generally use auto_ptr at threading interfaces (so that passing
the pointer to a different thread makes the object inaccessible
in the original thread), and not much else; I use the Boehm
collector for memory management. In legacy applications, where
I can't always use the Boehm collector, I'll use a mixture of my
own (invasive) reference counted pointer, something along the
lines of scoped_ptr, and auto_ptr, depending on the context and
the semantics I need. (My legacy applications all have to be
compiled with older compilers, so Boost is out. But I think I'd
still go with my invasive reference counted pointer, as being
somewhat less fragile than boost::shared_ptr.)
Alternatively, I've been experimenting with changing some existing raw
pointers into either:
CheckedPointer<T> or
OptionalPointer<T>
Where the former requires (and asserts) that its pointer never be null,
and the latter allows a null pointer, but asserts that a null pointer is
never dereferenced -- any feedback on this idea is appreciated :).
I don't see much advantage in the second; all of the systems I
work on fail when a null pointer is dereferenced anyway. The
former might be interesting in some contexts, although the name
is a little vague; maybe NonNullPointer<T>?
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]