PROGRAMMING IN C/Student Engineering Notebook
Page 07·notes
STRUCTURES, UNIONS & MEMORY ALIGNMENT—~30 MIN READ

Structure Padding, Memory Alignment & Dynamic Allocation

“Natural word alignment, member reordering optimization, and heap management.”

1.The Hardware Necessity of Structure Padding

Modern CPUs access memory in multi-byte words (e.g. 4 bytes on 32-bit systems, 8 bytes on 64-bit systems). If a 4-byte integer begins at an unaligned address (e.g., address 1001), the CPU must perform TWO separate memory read cycles and stitch the bytes together in registers.

To optimize performance, C compilers insert unused filler bytes ('padding') between structure members so that every member begins at an address divisible by its natural size.

Structure Alignment Rules (for a standard 32-bit architecture):

1. Member Alignment: A primitive data type of size k bytes must start at a byte offset that is a multiple of k (char [1 byte]: any offset; short [2 bytes]: multiple of 2; int/float [4 bytes]: multiple of 4; double [8 bytes]: multiple of 4 or 8).

2. Total Structure Alignment: The total size of the structure must be an integer multiple of the LARGEST member's alignment requirement. Trailing padding is added at the end if needed.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

2. GATE Worked Numerical: Calculating sizeof(struct)

⚠️ Pitfall Warning:

Don't forget the trailing padding at the end of the structure! The total size must be a multiple of the largest member's size.

Consider the structure on a 32-bit machine where sizeof(char)=1, sizeof(short)=2, sizeof(int)=4:

struct Node {

char a;

int b;

short c;

};

Find sizeof(struct Node).

Step 1: Lay out members sequentially:

- 'char a' takes 1 byte at Offset 0.

- Next member is 'int b' (size 4). It MUST start at an offset divisible by 4.

- Offsets 1, 2, 3 cannot hold 'b'. The compiler inserts 3 padding bytes!

- 'int b' occupies Offsets 4, 5, 6, 7 (4 bytes).

- Next member is 'short c' (size 2). It starts at Offset 8 (8 is divisible by 2).

- 'short c' occupies Offsets 8, 9 (2 bytes).

Step 2: Check total structure size so far:

Offsets 0 to 9 = 10 bytes used.

Step 3: Apply Total Structure Alignment Rule:

Largest member is 'int b' (size 4 bytes).

Total structure size MUST be an integer multiple of 4.

Next multiple of 4 after 10 is 12.

Compiler inserts 2 trailing padding bytes at Offsets 10 and 11.

Final sizeof(struct Node) = 12 bytes.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: Sort members from largest to smallest alignment to reduce padding. Exact sizes depend on the platform.

Worked example: structure padding

Problem Statement:
Assuming 4-byte ints aligned to 4 bytes: what is sizeof(struct { char c; int i; char d; })? And after reordering to { int i; char c; char d; }?
Step 1: Original order

c at offset 0; 3 bytes of padding so i starts at offset 4; i fills 4 to 7; d at 8.

Step 2: Tail padding

The size must be a multiple of the largest alignment (4): 9 rounds up to 12.

↳ Result: 12 bytes
Step 3: Reordered

i at 0 to 3, c at 4, d at 5; 6 rounds up to 8.

↳ Result: 8 bytes
Final Answer:12 bytes, versus 8 after reordering
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · GATE-LEVEL

Same assumptions (4-byte int, aligned to 4). What is sizeof(struct { char a; char b; int c; })?

Problem 2 · FOUNDATION

What does malloc return when it can't allocate the memory?

Problem 3 · GATE-LEVEL

Using a pointer after calling free on it is:

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
The C Programming Language (2nd Edition) ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Neso Academy · C Programming Full Course↗Instructor: Neso Academy · Covers: Data types, pointers, arrays, functions, recursion, file I/O

Systematic beginner-to-advanced C with clear examples.

Open YouTube↗
Jenny's Lectures CS/IT · C Programming↗Instructor: Jenny's Lectures · Covers: Arrays, strings, pointers, structures, file handling, preprocessor

Clear step-by-step C explanations — great for semester lab preparation.

Open YouTube↗