0
0
C++programming~10 mins

Union basics in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Union basics
Declare union variable
Assign value to one member
Access member value
Note: all members share same memory
Assign value to another member
Access members again
Observe that last assignment overwrites previous
A union lets you store different types in the same memory space, but only one at a time. Assigning to one member overwrites others.
Execution Sample
C++
union Data {
  int i;
  float f;
};

Data d;
d.i = 10;
float x = d.f;
This code declares a union with int and float, assigns 10 to int member, then reads float member.
Execution Table
StepActionMember Assigned/ReadValue StoredValue ReadNote
1Declare union variable d---Memory allocated for largest member (float or int)
2Assign d.i = 10i10 (int bits)-int member set, memory holds int bits for 10
3Read d.ii1010Reading int member returns 10
4Read d.ff10 (int bits)1.4e-44 (float)Reading float member interprets int bits as float
5Assign d.f = 5.5f5.5 (float bits)-float member set, overwrites memory
6Read d.ff5.55.5Reading float member returns 5.5
7Read d.ii5.5 (float bits)1085276160 (int)Reading int member interprets float bits as int
8End---Program ends
💡 Program ends after demonstrating union member assignments and reads
Variable Tracker
VariableStartAfter Step 2After Step 5Final
d.iundefined10bit pattern of 5.5 floatbit pattern of 5.5 float
d.fundefinedbit pattern of 10 int5.55.5
Key Moments - 3 Insights
Why does reading d.f after assigning d.i give a strange number?
Because d.i and d.f share the same memory, reading d.f interprets the int bits as a float (see execution_table step 4).
What happens when we assign d.f after assigning d.i?
The float assignment overwrites the shared memory, so the int value is lost (see execution_table step 5).
Can we store int and float values at the same time in a union?
No, only one member's value is valid at a time because all members share the same memory space.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does d.f hold immediately after step 2?
A5.5 as a float
B10 as a float (1.4e-44)
C10 as an int
DUndefined
💡 Hint
Check step 4 in execution_table where d.f reads the bits of d.i = 10
At which step does the union memory get overwritten with a float value?
AStep 3
BStep 2
CStep 5
DStep 7
💡 Hint
Look at the Action column for assignment to d.f in execution_table
If we read d.i after step 5, what happens?
AWe get the int bits of 5.5 float interpreted as int
BWe get the int 10
CWe get 5.5 as int
DUndefined behavior
💡 Hint
See step 7 in execution_table for reading d.i after assigning d.f
Concept Snapshot
union Data {
  int i;
  float f;
};
- All members share the same memory.
- Assigning one member overwrites others.
- Reading a different member interprets the same bits differently.
Full Transcript
This visual trace shows how a union in C++ shares memory between its members. We declare a union with int and float members. Assigning to one member stores bits in shared memory. Reading another member interprets those bits differently. For example, assigning 10 to the int member and then reading the float member shows a strange float value because the bits represent an int 10, not a float 10. Assigning a float value overwrites the memory, so reading the int member then shows the int interpretation of the float bits. This demonstrates that only one member's value is valid at a time in a union.