dmd icon indicating copy to clipboard operation
dmd copied to clipboard

placement new returns bad pointer

Open TurkeyMan opened this issue 7 months ago • 1 comments

This is really strange!

enum Bytes = 16; // set this to whatever you like

struct S {
    ubyte[Bytes] poo;
}

void main()
{
    ubyte[Bytes] buf = void;
    S* t = new(buf) S();

    assert(cast(ubyte*)t == buf.ptr); // FAILS!

    assert(cast(ubyte*)t == buf.ptr + Bytes*2); // THIS WORKS! (this should NOT be correct)
}

For reasons unknown; t is equal to buf.ptr + Bytes*2... why would this new expression produce a result with offset of exactly Bytes*2 from the target? If you change Bytes, you'll notice the offset is always Bytes*2

TurkeyMan avatar Apr 13 '25 15:04 TurkeyMan

@WalterBright here's another super weird one...

TurkeyMan avatar Apr 13 '25 15:04 TurkeyMan