OK to do bit-packing with GC pointers?

H. S. Teoh hsteoh at qfbox.info
Fri Jul 22 17:03:30 UTC 2022


On Fri, Jul 22, 2022 at 04:50:44PM +0000, Ben Jones via Digitalmars-d-learn wrote:
> I'm looking to store a pointer to one of 2 unrelated (no inheritance
> relationship) classes and use the LSb to track which type I have.  Is
> this going to cause any problems with the GC?  For one of the classes
> I'll have a "pointer" to 1 byte past the start of the object.  It
> seems like std.bitmanip.taggedClassRep does something similar, so I
> assume it's OK, but wanted to double check.

https://dlang.org/spec/garbage.html#pointers_and_gc

Section 28.3, paragraph 3 "Undefined Behaviour", 4th item:

	Do not take advantage of alignment of pointers to store bit
	flags in the low order bits:

	p = cast(void*)(cast(int)p | 1);  // error: undefined behavior


T

-- 
Never step over a puddle, always step around it. Chances are that whatever made it is still dripping.


More information about the Digitalmars-d-learn mailing list