Unions vs Structures & Bit-Fields Memory Optimization
Unit 4: Structures, Unions, Enums & Storage Classes • Problem Solving Using C
👨🏫 Professor's Mental Model: The Private Apartment Suite vs Shared Single-Bed Room
Structure ek aisi building hai jisme har member ka apna alag personal bedroom hai: sab log ek saath apne kamre me reh sakte hain (Separate Memory). Lekin Union ek aisa hotel room hai jisme sirf EK HI BED hai: chahe int soye, float soye ya char soye—ek waqt me sirf ek hi mehmaan reh sakta hai! Naya mehmaan aate hi purana bahar fenk diya jata hai (Memory Overwritten)! Isliye Union ki total memory sirf uske sabse bade member ke barabar hoti hai!
Academic Lecture Notes & Solved Study Pages
Unit 4 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. Union: Definition, Syntax & Shared Memory Principle
union Record {
int i; // 4 Bytes
float f; // 4 Bytes
char ch; // 1 Byte
};
Only the most recently written member retains valid data. Mutating `r.f = 3.14f` corrupts whatever integer was previously stored in `r.i` because they occupy the exact same 4 bytes of silicon memory.
2. Real-World Applications of Unions
3. Bit-Fields: Compacting Boolean Flags to Single Bits
In C, the smallest primitive type (`char`) occupies 8 bits. If you only need to store a 1-bit boolean flag (0 or 1), 7 bits are wasted!
struct DeviceStatus {
unsigned int is_ready : 1; // Consumes exactly 1 bit
unsigned int error_code : 3; // Consumes 3 bits (values 0 to 7)
unsigned int mode : 2; // Consumes 2 bits (values 0 to 3)
};
| Parameter | Structure (struct) | Union (union) |
|---|---|---|
| Keyword | struct | union |
| Memory Allocation | Separate memory space allocated for every individual member | Shared common memory space for all members |
| Total Byte Size | Greater than or equal to sum of all member sizes | Equal to the size of its single largest member (+ alignment) |
| Member Coexistence | All members can hold valid, active data simultaneously | Only ONE member can hold valid, active data at any instant |
| Alteration Side-Effect | Modifying one member has ZERO effect on others | Modifying one member corrupts/overwrites previous member data |
| Member Memory Address | Each member has a distinct, increasing memory address | All members share the EXACT same base memory address |
Interactive Tested Code Example
#include <stdio.h>
union DataPacket {
int integer_val;
float float_val;
char char_val;
};
int main(void) {
union DataPacket packet;
printf("=== UNION SHARED MEMORY ARCHITECTURE ===\n");
printf("sizeof(packet): %zu Bytes (Size of largest member: float/int)\n\n", sizeof(packet));
// 1. Assigning Integer
packet.integer_val = 100;
printf("Assigned packet.integer_val = 100\n");
printf("Integer: %d | Float (Corrupt): %f\n\n", packet.integer_val, packet.float_val);
// 2. Assigning Float (Overwrites Integer!)
packet.float_val = 98.75f;
printf("Assigned packet.float_val = 98.75\n");
printf("Float: %.2f | Integer (Corrupted bits): %d\n", packet.float_val, packet.integer_val);
return 0;
}🎯 University Exam Scoring Blueprint
- In university exams, Structure vs Union is a perennial 10-mark question; always reproduce the 6-parameter table.
- Draw memory boxes contrasting separate contiguous slots (struct) with a single overlapping box (union).
- Mention that address-of operator (&) cannot be used on bit-field members because memory addresses are byte-aligned, not bit-aligned.