On Tuesday, 4 June 2024 at 16:58:50 UTC, Basile B. wrote:
question in the header, code in the body, execute on a X86 or X86_64 CPU

```d
module test;

void setIt(ref bool b) @safe
{
    b = false;
}

void main(string[] args)
{
    ushort a = 0b1111111111111111;
    bool* b = cast(bool*)&a;
    setIt(*b);
    assert(a == 0b1111111100000000); // what actually happens
    assert(a == 0b1111111111111110); // what would be safe
}
```

I understand that the notion of `bool` doesn't exist on X86, hence what will be used is rather an instruction that write on the lower 8 bits, but with a 7 bits corruption.

Do I corrupt memory here or not ?
Is that a safety violation ?

No everything is fine.
The bool is the same size like byte or char.
So your cast makes &a pointer to a byte.
And this byte has to be made completely zero by setIt, otherwise it would not be false in the sense of bool type.

Reply via email to