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
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.
#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:
- The union
Data
is defined with two members:intValue
(an integer) andfloatValue
(a float). - When
intValue
is assigned a value, it occupies the shared memory location. - When
floatValue
is assigned a value, it overwrites the shared memory, causing the value ofintValue
to change. - 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.
#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:
- The union
Converter
is defined with two members:floatValue
andintValue
. - The
floatValue
is assigned the value3.14f
, which is stored in memory. - When
intValue
is accessed, it interprets the binary representation of3.14f
as an integer. - 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.
#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:
- The union
Efficient
contains members of typesint
,double
, andchar
. - The size of the union is determined by its largest member, which is
double
(8 bytes). - All members share the same memory space, making the union memory-efficient compared to structures.
Key Points to Remember about union
Keyword
- A
union
allows multiple members to share the same memory location. - The size of a union is equal to the size of its largest member.
- Only one member of a union can hold a valid value at any given time.
- Unions are commonly used for memory-efficient data storage, type punning, and low-level programming tasks.
- Accessing a member of a union that was not last assigned can lead to undefined behavior.