C++ and garbage collection

Florian Weimer fw@deneb.enyo.de
Sun Feb 6 19:29:00 GMT 2011


* Enrico Weigelt:

>> GCC includes a tr1::shared_ptr so there's no extra dependency if you
>> are only planning to use g++, and std::shared_ptr is part of C++0x.
>
> How does that one actually work and what do I have to do to use it ?

What's the oldest GCC version you're targeting?

Modern GCC has std::unique_ptr (zero-overhead pointer wrapper with
RAII semantics), std::shared_ptr and std::make_shared (avoiding a
separate allocation using operator new).  This should work after some
adjustments, as long as your data structures are acyclic.  You just
have to avoid taking short-cuts to optimize things, using references
and plain pointers instead of the std:: wrappers.  In this regard,
Boehm GC is hard to beat.



More information about the Gcc-help mailing list