0
0
Cprogramming~10 mins

Structure vs union comparison - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Structure vs union comparison
Declare struct with multiple members
Memory allocated = sum of all members' sizes
Each member has its own memory space
Declare union with multiple members
Memory allocated = size of largest member
All members share the same memory space
Assign value to one union member
Reading other union members shows overlapping data
Structures allocate separate memory for each member, unions share the same memory for all members.
Execution Sample
C
struct Example {
  int a;
  float b;
};

union ExampleU {
  int a;
  float b;
};
Defines a struct with two members and a union with the same two members.
Execution Table
StepActionMemory AllocationMember ValuesNotes
1Declare struct ExampleSize = sizeof(int) + sizeof(float)a = uninitialized, b = uninitializedStruct allocates separate memory for each member
2Assign struct.a = 10Same as abovea = 10, b = uninitializedValue stored in struct.a's memory
3Assign struct.b = 3.14Same as abovea = 10, b = 3.14Value stored in struct.b's memory
4Declare union ExampleUSize = max(sizeof(int), sizeof(float))a = uninitialized, b = uninitializedUnion allocates shared memory for all members
5Assign union.a = 10Same as abovea = 10, b = overlapping bits of 10Value stored in shared memory
6Read union.bSame as abovea = 10, b = value from same memory bits as aReading b shows overlapping data, may be garbage or interpreted bits
7Assign union.b = 3.14Same as abovea = bits of 3.14, b = 3.14Value stored in shared memory, overwriting a
8Read union.aSame as abovea = value from bits of 3.14, b = 3.14Reading a shows overlapping data, may be garbage or interpreted bits
9End--Execution ends
💡 Program ends after demonstrating memory differences between struct and union
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6After Step 7After Step 8Final
struct.auninitialized1010----10
struct.buninitializeduninitialized3.14----3.14
union.auninitialized--1010bits of 3.14bits of 3.14bits of 3.14
union.buninitialized--overlapping bits of 10overlapping bits of 103.143.143.14
Key Moments - 3 Insights
Why does assigning to union.a affect union.b's value?
Because union members share the same memory, changing one member overwrites the shared space, affecting all members (see steps 5 and 6 in execution_table).
Why can struct.a and struct.b hold different values at the same time?
Struct allocates separate memory for each member, so each can store independent values simultaneously (see steps 2 and 3 in execution_table).
What happens when you read a union member different from the last assigned one?
You read the same memory bits interpreted as that member's type, which may produce unexpected or garbage values (see steps 6 and 8 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the values of struct.a and struct.b?
Aa = 10, b = 3.14
Ba = 3.14, b = 10
Ca = uninitialized, b = 3.14
Da = 10, b = uninitialized
💡 Hint
Check the 'Member Values' column at step 3 in execution_table
At which step does union.b first get a meaningful assigned value?
AStep 5
BStep 7
CStep 6
DStep 8
💡 Hint
Look for when union.b is explicitly assigned a value in execution_table
If you assign union.b = 5.5 at step 7 instead of 3.14, what happens to union.a at step 8?
Aunion.a holds 5.5 as int
Bunion.a remains unchanged
Cunion.a holds bits of 5.5 interpreted as int
Dunion.a becomes zero
💡 Hint
Recall union members share memory; see variable_tracker for union.a after step 7
Concept Snapshot
Structure vs Union in C:
- struct allocates separate memory for each member.
- union allocates shared memory equal to largest member.
- struct members hold independent values simultaneously.
- union members share memory; last write affects all.
- Reading different union member than last written may show unexpected data.
Full Transcript
This visual execution compares structures and unions in C. Structures allocate separate memory for each member, so each can hold independent values at the same time. Unions allocate shared memory equal to the largest member, so all members share the same space. Assigning a value to one union member overwrites the shared memory, affecting all members. Reading a union member different from the last assigned one may produce unexpected values because the bits are interpreted differently. The execution table shows step-by-step how values are assigned and how memory is allocated differently for struct and union. The variable tracker follows the changes in member values after each step. Key moments clarify common confusions about memory sharing and value overwriting in unions versus independent storage in structures.