J
James Kanze
std::list<>
or boost::intrusive::list<>
or struct node { smart_pointer<node> next; };
or explicit destruction as Alberto described
or allocation from a pool which is all deallocated together
or garbage collection.
All have their advantages and disadvantages. Personally I'm
happy with std::list 99% of the time. I'm actually quite
curious to know what sort of applications or users use garbage
collection in C++, but I fear that it's the sort of discussion
that can deteriorate....
Not really. There are those of us who are paid to produce
working code as inexpensively as possible. Garbage collection
saves some work in some specific cases (which do occur fairly
frequently, even if they don't represent the majority of dynamic
allocations), so we use it. Not doing so would be
irresponsible, from a professional point of view---something
like using <generic.h> instead of templates.
Naturally, of course:
-- objects with value semantics shouldn't be allocated
dynamically to begin with, so garbage collection won't
affect them,
-- even more obviously, the same thing holds for RAII
mangagers,
-- and generally, entity objects should manage their own
lifetimes---although using garbage collection here does
improve type safety, and allows catching some errors
(dangling pointers).
But there will usually be a few odd objects which don't fit into
one of the above categories, and garbage collection means one
less thing to code when using them (and the detection of
dangling pointers for entity objects isn't to be sneezed at if
you need robustness).