union default initialization values

Nick Treleaven nick at geany.org
Wed Dec 6 12:43:00 UTC 2023


On Wednesday, 6 December 2023 at 12:38:35 UTC, Nick Treleaven 
wrote:
>> Correct. So I expected a NaN output for x. However, I wasn't 
>> expecting lo == 13835058055282163712 and hi == 32767 where x 
>> is of type real, or lo == 9221120237041090560 and hi = 0 where 
>> x is of type double. Based on the default initialization 
>> rules, I expected both lo and hi to have a value of zero 
>> regardless if x is of type double or real. This is what I'm 
>> trying to understand, how are these values derived?
>
> ulong.sizeof is 8, like double.sizeof. So F.init.lo should have 
> the same bit pattern as F.init.x because they share storage 
> exactly. F.init.hi should be 0 and it is on my system. (*"If 
> the union is larger than the first field, the remaining bits 
> are set to 0"*). I don't know why you don't get zero for that.

Oops, you said you did get zero in that case, so all is well. 
When `x` is `real`, that may overlap with `F.hi` because `real` 
can be more than 8 bytes.


More information about the Digitalmars-d-learn mailing list