

A garbage collector for C and C++ - dfox
http://www.hpl.hp.com/personal/Hans_Boehm/gc/

======
kmavm
I've investigated this for an industrial-strength, large-scale project in the
past. It's technologically interesting; to collect, it "stops the world"
(freezing all threads but one using, e.g., POSIX signals) and then uses the
CPU registers, each thread's stack, and global variables as "roots" to be
traversed to find live objects. In a way, this is even more general than just
C or C++; it really works for any static language, including assembly. One
caveat is that if your code represents pointers in non-obvious ways, the
objects referred to will look like garbage to this collector. I'm afraid it's
not as unusual as one might hope for low-level code to represent pointers in
funny ways...

------
wheels
GCC has used garbage collection internally for C for the last 8 years.

~~~
shadytrees
Cool, this was news to me. I found this link, if anybody else is interested:

<http://gcc.gnu.org/wiki/Memory_management>

------
matthavener
Is anybody using this? Experiences?

~~~
bianco
ECL (a mature, stable Lisp which compiles to C) uses it.

~~~
woadwarrior01
Mono uses it as well.

------
pfedor
An alternative approach to garbage collection in C++ (precise as opposed to
conservative): <http://smieciuch.sourceforge.net/>

~~~
anamax
You can't do precise GC in C++ because you can't find all valid pointers.

Two of the "can't handle" cases are: (1) the XOR trick to represent the
prev/next pointer for a doubly-linked list with a single field. (2) Ullman's
set set representation which uses an array of values which may, or may not, be
valid pointers. You don't know without consulting another structure. (The
whole point of this representation is that you don't clear things on
initialization.)

