Union is a special data type that stores different values in the same memory area. In a union, all members occupy the same address in memory, and the memory size is equal to the size of the largest member.
Usage:
Example:
union Data { int i; float f; char s[4]; }; union Data d; d.i = 0x41424344; // now 4 bytes in memory can be read as int, float, string printf("%c%c%c\n", d.s[0], d.s[1], d.s[2]); // vendor-specific output
Pitfalls and rules of usage:
Question: What is the point of using a union when you can simply use a structure with multiple fields?
Answer: Using a union saves memory because at any moment, it only holds ONE value, not all at once. In a structure, memory is allocated for each field, while in a union, only for the largest field; the others "share" this memory. Also, a union allows safe or intentional conversion between different data representations in one memory fragment.
Example:
struct S { int i; float f; } s; // sizeof = sizeof(int) + sizeof(float) union U { int i; float f; } u; // sizeof = max(sizeof(int),sizeof(float))
Story
In a device driver project, the connection with the "hardware" was done via a union with bit access to data. After some refactoring, the developer started writing to the wrong union field, which led to reading stale data and fatal system crashes because only one field is "real" at any moment in a union.
Story
When exchanging network packets through a union for memory management, the developer forgot to account for structure alignment. As a result, a one-byte offset occurred, and the structure was parsed with incorrect offsets, making the protocol incompatible with the original.
Story
While working on a serialization library, the programmer passed a union by value to a function, where the necessary field was not initialized before reading. As a result, the data was serialized incorrectly, junk appeared in the output stream, and it was impossible to restore the original information.