Structures (struct) & Architecture Memory Alignment
Unit 4: Structures, Unions, Enums & Storage Classes • Problem Solving Using C
👨🏫 Professor's Mental Model: The Multi-Compartment Bento Box vs Uniform Egg Carton
Array ek uniform ande ki tray (egg carton) jaisa hai jisme har slot bilkul ek jaisa hota hai (sirf int ya float). Lekin real life me agar ek Student ka record banana ho, toh usme Roll No (int), Name (string), aur Marks (float) sab ek sath chahiye! Yahan kaam aata hai Structure (struct)—ek multi-compartment bento lunch box ki tarah jisme roti, sabzi, aur mithai alag-alag aakaar ke hote hue bhi ek hi box ke andar safely pack rehte hain!
Academic Lecture Notes & Solved Study Pages
Unit 4 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. Structure Definition, Template & Variable Declaration
struct Student {
int roll_no; // 4 Bytes
char name[30]; // 30 Bytes
float marks; // 4 Bytes
};
`struct Student s1 = {101, "Rahul Kumar", 88.5f};`
Direct member assignment uses the Dot Operator (`.`): `s1.roll_no = 102;`.
2. The Dot Operator (.) vs Member Selection
3. Structure Padding & Memory Alignment Rules
Why does `sizeof(struct Demo)` not always equal the mathematical sum of its members?
Modern 32-bit and 64-bit processors access memory in 4-byte or 8-byte 'words'. To accelerate data transfers, the compiler aligns variables at memory addresses that are multiples of their size.
Consider:
struct Example {
char c; // 1 byte
// 3 padding bytes inserted by compiler!
int i; // 4 bytes (must start at a 4-byte boundary)
};
Total size = 1 + 3 (padding) + 4 = 8 Bytes (NOT 5 bytes)!
To prevent padding and optimize for network packets or embedded storage, use `#pragma pack(1)` to force contiguous single-byte byte packing.
| Byte Offset | Allocated Member | Byte Size | Purpose & Architectural Reason |
|---|---|---|---|
| Byte 0 | char c | 1 Byte | Stores char data |
| Bytes 1, 2, 3 | [PAD] Slack Bytes | 3 Bytes | Inserted by compiler to align next integer to 4-byte boundary |
| Bytes 4, 5, 6, 7 | int i | 4 Bytes | Aligned on 4-byte word boundary for ultra-fast CPU bus fetching |
| Total Size | sizeof(struct Example) | 8 Bytes | Calculated structure size including padding |
Interactive Tested Code Example
#include <stdio.h>
struct Student {
int roll_no;
char name[20];
float marks;
};
struct PackedDemo {
char a;
int b;
};
int main(void) {
struct Student s1 = {101, "Aman Verma", 91.5f};
printf("=== STUDENT STRUCTURE RECORD ===\n");
printf("Roll No: %d\n", s1.roll_no);
printf("Name: %s\n", s1.name);
printf("Marks: %.1f%%\n\n", s1.marks);
printf("=== STRUCTURE PADDING DEMONSTRATION ===\n");
printf("sizeof(char): %zu | sizeof(int): %zu\n", sizeof(char), sizeof(int));
printf("Mathematical Sum: 1 + 4 = 5 Bytes\n");
printf("Actual sizeof(struct PackedDemo): %zu Bytes (Includes 3 Slack Padding Bytes)\n", sizeof(struct PackedDemo));
return 0;
}🎯 University Exam Scoring Blueprint
- In university exams, always explain Structure Padding with a byte-map diagram showing the slack bytes.
- Explain that the struct tag by itself does not allocate memory; memory is allocated only when structure variables are declared.
- Mention that structure variables of the same type can be copied directly using simple assignment: s2 = s1;.