What is sizeof for union U { char c[5]; double d; } on a platform where double needs 8-byte alignment?
Correct answer
A. 8, because the union is padded up to a multiple of its strictest member alignment
14 views
A. 8, because the union is padded up to a multiple of its strictest member alignment
14 views
Union size equals its largest member rounded up to the alignment of the strictest member, so a 5-byte array beside a double occupies 8 bytes with three bytes of tail padding. All members share the same starting address, which is why writing d and then reading c[0] exposes the machine's byte order. Adding member sizes describes a struct, not a union.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.