UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

2.3.  Unions

In a structure, each field has space in memory to store its value. But there are special situations in which the data structures may waste a lot of memory. Let us consider an application that identifies users by one of the following five possible data:

  • NIF: eight digits followed by a character.

  • CIF: character followed by 8 digits.

  • Passport: eight characters/digits.

  • NIE: character followed by 7 digits and a second character.

  • String of up to 16 characters.

A possible data structure to store this data is shown in the left side of the following figure:

Assuming that integers occupy 4 bytes and characters 1 byte, the structure requires 40 bytes to be stored in memory as shown in the right side of the previous figure. But out of all the fields, only one contains information, the rest are empty. This means that the data structure will only use between 12.5% and 40% of the space that occupies. More than half of the memory will be wasted.

C offers a structure in which all fields share the same space in memory and is conceived for those cases in which only one of the fields is used at each time. This structure is defined replacing the word struct by union. The following figure shows the user data definition using union.

When a union is defined, only the space for the largest of the fields is reserved. The data is stored starting in the same memory location and with the structure of the selected field. This construction does not store anywhere which of the fields is being used. If this information is needed, the programmer must store it in an additional data structure. The access to the data in a union is performed exactly as in a structure.