0
0
C++programming~10 mins

Structure vs union comparison in C++ - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Structure vs union comparison
Declare struct with multiple members
Each member gets its own memory
Assign values to members
Access members independently
Declare union with multiple members
All members share the same memory
Assign value to one member
Access members (overlapping data)
Observe how union stores only one value at a time
Structures allocate separate memory for each member, unions share the same memory for all members, so only one member holds a valid value at a time.
Execution Sample
C++
struct MyStruct {
  int a;
  float b;
};

union MyUnion {
  int a;
  float b;
};

MyStruct s = {10, 3.14f};
MyUnion u; u.a = 10;
This code declares a struct and a union, assigns values, and shows how memory is used differently.
Execution Table
StepActionMemory for structMemory for unionOutput/Note
1Declare struct MyStruct with int a and float ba: uninitialized, b: uninitializedN/AStruct members have separate memory
2Declare union MyUnion with int a and float bN/Ashared memory for a and bUnion members share same memory
3Assign s.a = 10a: 10, b: uninitializedN/AStruct a set to 10
4Assign s.b = 3.14a: 10, b: 3.14N/AStruct b set independently
5Assign u.a = 10N/Ashared memory = 10 (int)Union memory holds int 10
6Access u.bN/Ashared memory interpreted as floatValue of u.b depends on bit pattern of int 10
7Assign u.b = 3.14N/Ashared memory = 3.14 (float)Union memory holds float 3.14
8Access u.aN/Ashared memory interpreted as intValue of u.a depends on bit pattern of float 3.14
9EndStruct keeps both values intactUnion only keeps last assigned valueStruct stores all members; union stores one at a time
💡 Execution ends after demonstrating memory differences and access behavior
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 7Final
s.auninitialized1010101010
s.buninitializeduninitialized3.143.143.143.14
u.auninitializeduninitializeduninitialized10bit pattern of 3.14bit pattern of 3.14
u.buninitializeduninitializeduninitializedbit pattern of 103.143.14
Key Moments - 3 Insights
Why does changing one union member affect the others?
Because all union members share the same memory, assigning to one overwrites the data for all others, as shown in steps 5 to 8 in the execution_table.
Can a struct hold multiple values at the same time?
Yes, each struct member has its own memory space, so all members keep their values independently, as seen in steps 3 and 4.
Why does accessing a different union member after assignment give strange values?
Because the bits are interpreted differently depending on the member type, so reading u.b after assigning u.a shows the float interpretation of the int bits (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What value is stored in the union's shared memory?
AUninitialized
BFloat 3.14
CInteger 10
DBoth 10 and 3.14
💡 Hint
Check the 'Memory for union' column at step 5 in execution_table
At which step does the struct have both members assigned values?
AStep 4
BStep 3
CStep 5
DStep 7
💡 Hint
Look at 'Memory for struct' column in execution_table for when both a and b have values
If you assign a value to u.b after u.a, what happens to u.a's value?
AIt stays the same
BIt changes to reflect the bit pattern of u.b
CIt becomes uninitialized
DIt doubles
💡 Hint
Refer to steps 7 and 8 in execution_table and variable_tracker for u.a and u.b
Concept Snapshot
Structure vs Union in C++:
- struct: each member has its own memory
- union: all members share the same memory
- struct stores all member values simultaneously
- union stores only one member value at a time
- Accessing different union members interprets same bits differently
Full Transcript
This visual execution compares structures and unions in C++. Structures allocate separate memory for each member, so all members keep their values independently. Unions share the same memory for all members, so only one member holds a valid value at a time. Assigning to one union member overwrites the shared memory, affecting all members. Accessing a different union member after assignment shows the bits interpreted as that member's type, which can look like strange values. The execution table traces each step, showing how struct members keep their values separately, while union members overlap in memory. The variable tracker shows the values changing after each assignment. Key moments clarify why union members affect each other and why structs keep all values. The quiz tests understanding of memory usage and value changes in both types.