How does D distnguish managed pointers from raw pointers?

Adam D. Ruppe destructionator at gmail.com
Thu Oct 3 14:21:21 UTC 2019


On Thursday, 3 October 2019 at 14:13:55 UTC, IGotD- wrote:
> suggests that char *p is really a "fat pointer" with size 
> information.

D pointers are plain naked pointers. What that doc segment is 
saying is it works like C - in-bounds arithmetic will work, out 
of bounds is undefined behavior. You can do it, but it might 
crash you or whatever.

There's no difference in the language between a Gc pointer and 
any other pointer. But....

> How does D internally know that a pointer was previously 
> allocated by the GC or malloc?

But, this is a bit more nuanced. D, the language, does not know 
how it was allocated, there's no difference in the type system, 
but the runtime can figure it out based on the pointer value, if 
it falls inside the range of the GC's allocated area.

It does NOT use that for bounds checking though! It is just an 
internal detail it uses for some of the GC function to help its 
sweeps and some of the interface functions.

> If we would replace the GC with reference counting. How would D 
> be able to distinguish a reference counted pointer from a raw 
> pointer at compile time in order to insert the code associated 
> with the reference counting?

It won't, D reference counting is and then would have to be done 
by different types.


More information about the Digitalmars-d-learn mailing list