Are heap objects never moved by the garbage collector?

Jonathan M Davis jmdavisProg at gmx.com
Fri May 31 10:14:32 PDT 2013


On Friday, May 31, 2013 18:31:38 Carl Sturtivant wrote:
> "The D Programming Language" (TDPL) p.178 asserts the following.
> 
> "The objects themselves stay put, that is their locations in
> memory never change after creation."
> 
> I take this to mean that the D garbage collector doesn't move
> live objects and adjust all references to them the way that some
> garbage collectors do. That is to say, the addresses of objects
> are not changed by the garbage collector.
> 
> Does D guarantee this?

I don't believe that there is any such guarantee in the language. Structs on 
the heap get moved around all the time, so having objects with references to 
themselves is effectively disallowed. The GC does not currently ever move any 
objects, but I don't believe that the language itself guarantees that it never 
will. It may be that in the future it would, though it's unlikely given some 
of the technical difficulties caused by having unmanaged pointers and void* and 
whatnot. Languages which run in a VM and are restricted to managed pointers 
are able to make many more assumptions than D is, making it much easier for 
them to do moveable garbage collectors.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list