C++ union Keyword

The union keyword in C++ is used to define a special data type that allows multiple members to share the same memory location. Unlike structures (struct), in a union, only one member can be used at a time, as all members overlap in memory. This feature makes unions memory-efficient, as the size of a union is determined by the size of its largest member.

Unions are commonly used in low-level programming, such as handling hardware registers, type punning, and creating memory-efficient data types.


Syntax

</>
Copy
union UnionName {
    data_type1 member1;
    data_type2 member2;
    ...
};
union
The keyword used to define a union.
UnionName
The name of the union type.
data_type1, data_type2, …
The types of the members that share the same memory space.
member1, member2, …
The names of the union members.

Examples

Example 1: Basic Union Usage

This example demonstrates the basic usage of a union where members share the same memory.

</>
Copy
#include <iostream>
using namespace std;

union Data {
    int intValue;
    float floatValue;
};

int main() {
    Data data;

    data.intValue = 10; // Assign an integer value
    cout << "Integer value: " << data.intValue << endl;

    data.floatValue = 3.14; // Assign a float value
    cout << "Float value: " << data.floatValue << endl;

    cout << "Integer value after assigning float: " << data.intValue << endl;

    return 0;
}

Output:

Integer value: 10
Float value: 3.14
Integer value after assigning float: 1078523331

Explanation:

  1. The union Data is defined with two members: intValue (an integer) and floatValue (a float).
  2. When intValue is assigned a value, it occupies the shared memory location.
  3. When floatValue is assigned a value, it overwrites the shared memory, causing the value of intValue to change.
  4. This demonstrates that a union allows only one member to hold a meaningful value at a time.

Example 2: Using Unions for Type Conversion

This example demonstrates how unions can be used for type punning or converting between data types.

</>
Copy
#include <iostream>
using namespace std;

union Converter {
    float floatValue;
    int intValue;
};

int main() {
    Converter conv;

    conv.floatValue = 3.14f; // Assign a float value
    cout << "Float value: " << conv.floatValue << endl;
    cout << "Interpreted as integer: " << conv.intValue << endl;

    return 0;
}

Output:

Float value: 3.14
Interpreted as integer: 1078523331

Explanation:

  1. The union Converter is defined with two members: floatValue and intValue.
  2. The floatValue is assigned the value 3.14f, which is stored in memory.
  3. When intValue is accessed, it interprets the binary representation of 3.14f as an integer.
  4. This demonstrates how unions can be used for type punning, interpreting the same memory in different ways.

Example 3: Memory Efficiency of Unions

This example demonstrates how unions save memory by sharing the same memory space for all members.

</>
Copy
#include <iostream>
using namespace std;

union Efficient {
    int intValue;
    double doubleValue;
    char charValue;
};

int main() {
    cout << "Size of union: " << sizeof(Efficient) << " bytes" << endl;
    return 0;
}

Output:

Size of union: 8 bytes

Explanation:

  1. The union Efficient contains members of types int, double, and char.
  2. The size of the union is determined by its largest member, which is double (8 bytes).
  3. All members share the same memory space, making the union memory-efficient compared to structures.

Key Points to Remember about union Keyword

  1. A union allows multiple members to share the same memory location.
  2. The size of a union is equal to the size of its largest member.
  3. Only one member of a union can hold a valid value at any given time.
  4. Unions are commonly used for memory-efficient data storage, type punning, and low-level programming tasks.
  5. Accessing a member of a union that was not last assigned can lead to undefined behavior.