bool passed by ref, safe or not ?

Basile B. b2.temp at gmx.com
Tue Jun 4 16:58:50 UTC 2024


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 ?


More information about the Digitalmars-d-learn mailing list