×
>
<

C++ Programming

C++ Union | CrackEase

Union

Union in C++

A union is a special user-defined data type in C++ that allows storing different data types in the same memory location. Members of a union share the same memory region; at any time only one member contains a meaningful value.

Unions are similar to structures, but while each member of a struct occupies its own memory, union members overlap and the total size of a union equals the size of its largest member.

Union — quick facts
  • Use the union keyword to declare a union type.
  • All members share the same memory location (overlap).
  • The size of the union is determined by its largest member.
  • Only one member should be used at a time — writing to one member and reading another leads to implementation-defined behaviour (use carefully).

Defining a union

Syntax example:

union union_name
{
    data_type member1;
    data_type member2;
    ...
    data_type memberN;
};
union Employee
{
    int emp_id;
    char emp_name[30];
    float salary;
};

In this example, Employee is a union with three members: emp_id, emp_name and salary. They occupy the same memory location.

union diagram
Union variable declaration

You can declare union variables either alongside the union definition or later inside functions.

Declare variables with definition

union Teacher
{
    int teach_id;
    char teach_name[30];
} t1, t2;   // t1 and t2 are union variables

Declare variable in main()

union Teacher
{
    int teach_id;
    char teach_name[30];
};

int main()
{
    union Teacher info;
    return 0;
}
Union size (sizeof)

Because members share the same memory, the size of the union equals the size of its largest member (possibly plus padding). Let's see an example in C++ that prints sizes correctly.

#include <iostream>
#include <cstring>

union Teacher
{
    int teach_id;
    char teach_name[20];
    float salary;
};

int main()
{
    Teacher info;
    std::cout << "Size of teach_id : " << sizeof(info.teach_id) << " bytes\n";
    std::cout << "Size of teach_name: " << sizeof(info.teach_name) << " bytes\n";
    std::cout << "Size of salary    : " << sizeof(info.salary) << " bytes\n";
    std::cout << "Size of Teacher union: " << sizeof(info) << " bytes\n";
    return 0;
}
Sample output (example)
Size of teach_id : 4 bytes
Size of teach_name: 20 bytes
Size of salary    : 4 bytes
Size of Teacher union: 20 bytes

Union using pointer

Union members can be accessed through pointers using the -> operator. When using C-style string copy functions make sure to include <cstring> and copy safely (use size limits).

#include <iostream>
#include <cstring>

union Teacher
{
    int teach_id;
    char teach_name[30];
    float salary;
};

int main()
{
    Teacher info;
    Teacher *ptr = &info;

    ptr->teach_id = 34;
    // safe copy: limit to available space - 1 and null-terminate
    std::strncpy(ptr->teach_name, "Sanya", sizeof(ptr->teach_name) - 1);
    ptr->teach_name[sizeof(ptr->teach_name) - 1] = '\0';
    ptr->salary = 20000.00f; // writing salary overwrites previous members

    // Because union members overlap, reading a different member than the last written one
    // may produce unexpected/implementation-defined values. Demonstrated here for illustration.
    std::cout << "Teacher id is : " << ptr->teach_id << "\n";
    std::cout << "Teacher name is: " << ptr->teach_name << "\n";
    std::cout << "Teacher salary is : " << ptr->salary << "\n";

    return 0;
}

Possible output (depends on which member was last written):

Teacher id is : 
Teacher name is: Sanya
Teacher salary is : 20000.000000

Note: Because union members share memory, assigning to one member and then reading a different member is implementation-defined behaviour. Use unions primarily when you intentionally want overlapping storage (for example, in low-level code, variant representations, or to save memory), and ensure you read only the member that was most recently written, or use std::variant in modern C++ for safe type-safe alternatives.

Footer Content | CrackEase