bool passed by ref, safe or not ?

Olivier Pisano olivier.pisano at laposte.net
Wed Jun 5 05:15:42 UTC 2024


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
>
>
> 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 ?


The problem is that while setIt() is @safe, your main function is 
not. So the pointer cast (which is not @safe) is permitted.

A bool is a 1 byte type with two possible values : false (0) and 
true (1).

When you set the value to false, you write 0 to the byte it 
points to.

This is technically not a memory corruption, because as 
bool.sizeof < int.sizeof, you just write the low order byte of an 
int you allocated on the stack.


More information about the Digitalmars-d-learn mailing list