Lesson 2 of 5
Struct Padding & Alignment
The compiler inserts **padding bytes** between struct members to satisfy alignment requirements - each member must start at an address that is a multiple of that member's alignment (`char`: 1, `short`: 2, `int`: 4, `double`: 8). This means `sizeof(struct)` is often larger than the sum of members' sizes. Understanding padding prevents surprises when writing binary file formats or network packets.
Padding Rules & Layout Optimisation
#include <stdio.h>
#include <stddef.h> /* offsetof */
/* ── Layout A - bad: wastes 7 bytes of padding ─────────── */
struct BadLayout {
char a; /* offset 0 (1 byte) */
/* 7 bytes padding - double needs 8-byte alignment */
double b; /* offset 8 (8 bytes) */
char c; /* offset 16 (1 byte) */
/* 7 bytes padding - struct end must align to largest */
}; /* sizeof = 24 bytes (wasted 14!) */
/* ── Layout B - good: sorted largest to smallest ────────── */
struct GoodLayout {
double b; /* offset 0 (8 bytes) */
char a; /* offset 8 (1 byte) */
char c; /* offset 9 (1 byte) */
/* 6 bytes padding to align struct size to 8 */
}; /* sizeof = 16 bytes */
/* ── Layout C - packed: no padding (careful!) ─────────── */
struct __attribute__((packed)) PackedLayout {
char a; /* offset 0 */
double b; /* offset 1 - UNALIGNED! */
char c; /* offset 9 */
}; /* sizeof = 10 bytes - but SLOW on x86,
CRASH on strict-alignment architectures (ARM) */
/* ── offsetof - find where a member starts ─────────────── */
struct Example {
char x; /* 0 */
int y; /* 4 (padded from 1) */
char z; /* 8 */
double w; /* 16 (padded from 9) */
}; /* sizeof = 24 */
int main(void) {
printf("BadLayout: sizeof=%zu
", sizeof(struct BadLayout));
printf("GoodLayout: sizeof=%zu
", sizeof(struct GoodLayout));
printf("PackedLayout: sizeof=%zu
", sizeof(struct PackedLayout));
printf("
struct Example member offsets:
");
printf(" x @ offset %zu
", offsetof(struct Example, x)); /* 0 */
printf(" y @ offset %zu
", offsetof(struct Example, y)); /* 4 */
printf(" z @ offset %zu
", offsetof(struct Example, z)); /* 8 */
printf(" w @ offset %zu
", offsetof(struct Example, w)); /* 16 */
printf(" sizeof = %zu
", sizeof(struct Example)); /* 24 */
/* ── Alignment rule summary ─────────────────────────────
char: 1-byte alignment (no padding needed)
short: 2-byte alignment
int: 4-byte alignment (most common)
float: 4-byte alignment
double: 8-byte alignment
pointer: 8-byte alignment (64-bit)
Struct overall alignment = largest member's alignment.
Struct size = multiple of overall alignment. */
return 0;
}Common Mistakes
- Writing packed structs to binary files and reading on a different architecture - `__attribute__((packed))` removes padding but creates unaligned accesses. On x86 these are slow; on ARM they fault. Use explicit padding or serialise member by member.
- Assuming `sizeof(struct)` equals the sum of member sizes - always use `sizeof` or `offsetof` to get real sizes and offsets. Never hardcode struct layout in binary I/O.
- Reordering struct members without checking sizeof - changing member order changes padding. Structs written to disk/network become incompatible. Always version binary formats.
Tip
Tip
Practice Struct Padding Alignment in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Reordering struct members from largest to smallest eliminates alignment padding waste
Practice Task
Note
Practice Task - (1) Write a working example of Struct Padding Alignment from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Struct Padding Alignment is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- The compiler inserts **padding bytes** between struct members to satisfy alignment requirements - each member must start at an address that is a multiple of that member's alignment (`char`: 1, `short`: 2, `int`: 4, `double`: 8).
- Writing packed structs to binary files and reading on a different architecture - `__attribute__((packed))` removes padding but creates unaligned accesses. On x86 these are slow; on ARM they fault. Use explicit padding or serialise member by member.
- Assuming `sizeof(struct)` equals the sum of member sizes - always use `sizeof` or `offsetof` to get real sizes and offsets. Never hardcode struct layout in binary I/O.
- Reordering struct members without checking sizeof - changing member order changes padding. Structs written to disk/network become incompatible. Always version binary formats.
Finished this lesson?