ProgrammingEmbedded/Low-level C Developer

Explain how unions work in the C language. What tasks are solved with their help, how to use them correctly, and what are the typical pitfalls encountered when using them?

Pass interviews with Hintsage AI assistant

Answer

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:

  • Convenient for saving memory if a variable at one time holds one of several types of values;
  • Allows interpreting the same bits in different ways (for example, for low-level access or protocols).

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:

  • Keep only one "valid" field in the union at any one time.
  • There is no automatic tracking of which field was "last active."
  • Reading from an inactive field is undefined behavior.
  • The byte order on the platform is very important (endian!)

Trick question

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))

Examples of real mistakes


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.