Re: Is this good style of C++?
From: Dave O'Hearn (daveoh77_at_pobox.com)
Date: 12/06/04
- Next message: Dietmar Kuehl: "Re: serialize ofstring to stream"
- Previous message: vaness: "network printer IP address"
- In reply to: Ioannis Vranos: "Re: Is this good style of C++?"
- Next in thread: Ioannis Vranos: "Re: Is this good style of C++?"
- Reply: Ioannis Vranos: "Re: Is this good style of C++?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Dietmar Kuehl: "Re: serialize ofstring to stream"
- Previous message: vaness: "network printer IP address"
- In reply to: Ioannis Vranos: "Re: Is this good style of C++?"
- Next in thread: Ioannis Vranos: "Re: Is this good style of C++?"
- Reply: Ioannis Vranos: "Re: Is this good style of C++?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|