Re: Is this good style of C++?

From: Dave O'Hearn (daveoh77_at_pobox.com)
Date: 12/06/04


Date: 6 Dec 2004 09:40:27 -0800

Ioannis Vranos wrote:
> Dave O'Hearn wrote:
>
> > Almost! A reference-counted smart pointer, like Boost shared_ptr,
> > does not solve the object ownership problem. It just changes its
> > form from manual new/delete to something like a reference-counted
> > GC. This eliminates dangling pointers, but it does not eliminate
> > all memory leaks. If you store a smart pointer away somewhere and
> > forget about it, you will leak memory.
> > [...]
>
> What about a vector with one element for single objects?

I can't think of any uses for that that aren't better-covered by
something else. Vector is good for managing an array, so you don't have
to remember to free the array or manually keep track of its size. For
individual objects, I can think of,

1. Just keep it by value on the stack
2. auto_ptr
3. a reference counted smart pointer, like Boost shared_ptr
4. some kind of "weak reference" like Boost weak_ptr

There is no general solution to the problem I complained about. Weak
references help sometimes. Smart pointers reduce common errors when
doing simple things, which is the majority of code, but the worst kind
of errors are still possible, only they take on a different form.

-- 
Dave O'Hearn


Relevant Pages

  • Re: Storing a tree in a std::list<>
    ... Dave wrote: ... > tree where a vertex contain pointers to its parent / children. ... that the standard actually requires it. ... adding/removing elements do not ...
    (comp.lang.cpp)
  • Re: delay_check and check_subject problem
    ... Dave wrote: ... > Any pointers as to where i should be looking and what for would be most ... I am not clued up on programming so dont really want to go ... is too lazy to trim his article, and it's wasting the time of all readers. ...
    (comp.mail.sendmail)
  • Re: Modifying through pointer-to-const
    ... Dave wrote: ... This would be legal if MvPeriod and GapInteval are pointers to non-const ... don't affect the state of DynGapRec (as far as the compiler is concerned, ... and providing non-const member functions to alter the data they ...
    (comp.lang.cpp)
  • Re: Modifying through pointer-to-const
    ... > Dave wrote: ... not the values of the pointers per se. ... > don't affect the state of DynGapRec (as far as the compiler is concerned, ... then the compiler will balk. ...
    (comp.lang.cpp)
  • Re: Real World Flunks (Re: Why is Object Oriented so successful)
    ... AndyW wrote: ... "modeling the real world". ... CADD screen closely matches the object pointers in a typical real- ... I think the fact one has a reference to it ...
    (comp.object)