H
Harald van Dijk
Harald said:Given
union {
double d;
char x[1000];
} u = { 0 };
u.d will be initialised to zero, but I was talking about the bytes that
follow u.d.
In that example, isn't the part of u.x not overlapping with u.d supposed
to be zero-filled as well?
No, it's not, and in the general case, it can't be:
union {
char x[sizeof(double) - 1];
double d;
} u = { 0 };
There isn't any way to zero-initialise the final byte of d. (I'm assuming
sizeof(double) is not 1.)
This example could be inefficient if all-bits-zero wasn't 0.0, forcing
the entire union into .data instead of .bss, but I don't think you'd get
"random bytes" in the non-overlapping part of u.x.
If real-world compilers don't clear out bits in floating point constants
that don't contribute to the value (admittedly, this is not by a
conforming compiler, but it's not one of the areas in which it doesn't
conform), it wouldn't surprise me if real-world compilers don't clear out
bytes in unions that don't contribute to the value.