[OT] Garbage collection and pointers, was Re: Who modified my local variable?
levi at cold.org
Tue Jun 13 21:56:56 MDT 2006
On Jun 13, 2006, at 11:06 AM, Shane Hathaway wrote:
> Interesting. To me, the distinction between a pointer and a reference
> is that a reference not only points to the object referenced but also
> has some kind of strong link back to the thing that's referencing it.
> That strong link backward is what makes garbage collection
> possible. In
> the C++ code "int foo(int &bar)", the variable bar is just a cleverly
> disguised pointer, not a reference in the usage I am accustomed to.
I meant to reply to this earlier, but I just remembered about it.
Actually, back-links are not necessary to implement garbage
collection. All that's required is a knowledge at runtime of what
pieces of memory are really pointers. This isn't even strictly
necessary; there are what's known as conservative garbage collectors
that will guess whether a piece of data is a pointer or not based on
its bit pattern. This method can be used to retrofit garbage
collectors to C or C++ programs, though they will sometimes hold on
to small bits of memory that are not really necessary due to pieces
of data that happen to look like pointers but are not.
Since the only type information C has at runtime is the byte size of
a piece of data, conservative collectors are the only possibility for
straight C. Since C++ remains backward compatible with C, there
remain pieces of data that are pointers but are not identifiable as
such based on runtime information. So, conservative garbage
collection is the only method possible in those languages without
explicitly designing a new allocation system and using it exclusively
(or at least wherever garbage collection is desired).
More information about the PLUG