Garbage collected pointers?

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 1 20:53:08 UTC 2018


On 3/1/18 3:33 PM, H. S. Teoh wrote:
> On Thu, Mar 01, 2018 at 02:52:26PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote:
> [...]
>> There are a few in there, which I think are over-the-top. Such as
>> "don't cast a pointer to a non-pointer",
> [...]
> 
> Isn't that necessary for a precise GC?

It's not strictly necessary for a precise GC. It's necessary for a 
copying GC. In that case, if the GC decides to move your object and 
update all the references, it would miss that one. And even there, it's 
not necessary that you refrain from casting, it's just necessary to 
refrain from using that as an actual pointer in the future. I can't see 
how you might print a pointer value without casting it to an integer ;)

What is more important is that you don't use that as the ONLY reference 
to the object.

> 
> Also, AIUI the current GC already does not scan size_t[] blocks for
> pointers, so if you cast a pointer to size_t and put it in such an array
> with no other references to your object, you stand the chance of the GC
> collecting your object while it is still live.

Again, the key is "no other references". The document referenced above 
just says you can't cast.

>> or "Do not take advantage of alignment of pointers to store bit flags
>> in the low order bits".
> [...]
> 
> Won't a precise GC scanning for pointers to aligned objects want to skip
> values that can't be an aligned pointer?  Though in D's case, being
> required to be conservative would negate this.

int[] x = [1,2,3];
void[] y = x;
y = y[1 .. $]; // misaligned pointer.

It's easy to do, and it's done all over the place (buffers anyone?).

-Steve


More information about the Digitalmars-d-learn mailing list