Understanding C++ union behaviour in this code-reading question
This is a medium-difficulty C++ question that tests your grasp of union semantics—specifically, how memory is shared among members and how that affects the values you observe at runtime. It is the kind of detail-oriented question that appears in technical interviews where language mastery matters.
To answer correctly, you need to reason about memory layout, member overlap, and the order and size of write operations. The question rewards candidates who can trace through code mentally, predict output, and explain why a union behaves the way it does, rather than those who simply guess. Understanding unions is particularly useful when working with low-level code, serialization, or bit manipulation.
- Memory layout and sizeof unions
- Shared storage and member aliasing
- Write order and observable state
- Differences between unions and structs