C Programming
Struct padding and memory alignment in C: why is sizeof bigger than I expected?
You'd expect a struct with a char, an int and a char to be 6 bytes; sizeof says 12. The extra 6 bytes come from the CPU's alignment rules. Here's why padding exists and how to control it.
While learning C, one day you try this experiment and the result surprises you:
#include <stdio.h>
struct Example {
char a;
int b;
char c;
};
int main(void) {
printf("%zu\n", sizeof(struct Example)); /* you expect 6, it prints 12 */
}One char (1 byte), one int (4 bytes), one char (1 byte): 6 in total. But sizeof says 12. Where did the missing 6 bytes go? Nowhere, really; the compiler leaves them empty on purpose.
What is alignment?
CPUs like to read memory in blocks rather than byte by byte. When a 4-byte int sits at an address that's a multiple of 4, the CPU can read it in one go. If it doesn't, some architectures do two separate reads and combine them (slow), and others simply fault. That's why every type has an alignment requirement: usually its own size.
Padding: the gaps the compiler leaves
To place each field at a properly aligned address, the compiler inserts empty bytes between them. Here's the struct above in memory:
offset 0 : a (1 byte)
offset 1-3 : padding (3 bytes)
offset 4-7 : b (4 bytes, at a multiple of 4)
offset 8 : c (1 byte)
offset 9-11: padding (3 bytes, trailing)
total : 12 bytesWhy the trailing padding? Because when you make an array of these structs, the second element's b must also be at a multiple of 4. A struct's total size is rounded up to a multiple of its largest alignment requirement.
Shrinking by reordering fields
Order the same fields from largest to smallest and the padding shrinks:
struct Ordered {
int b; /* offset 0 */
char a; /* offset 4 */
char c; /* offset 5 */
}; /* sizeof = 8 */From 12 bytes down to 8; the same data in a third less memory. For a single struct it may seem trivial, but in an array of millions of elements it directly affects both memory and cache efficiency.
See it yourself with offsetof
#include <stddef.h>
printf("%zu %zu %zu\n",
offsetof(struct Example, a),
offsetof(struct Example, b),
offsetof(struct Example, c)); /* 0 4 8 */offsetof tells you how many bytes a field is from the start of the struct. The fastest way to understand alignment is to try it on your own structs.
Packed structs: when, and why carefully
Sometimes you don't want padding: if you're reading a file header or a network packet byte by byte, the struct's memory layout has to match the format exactly. In GCC and Clang __attribute__((packed)), and in MSVC #pragma pack(1), remove the padding.
struct __attribute__((packed)) Header {
unsigned char type;
unsigned int length; /* now at offset 1 */
}; /* sizeof = 5 */But it comes at a price: unaligned field access is slow on some CPUs and faults on others. Taking a field's address and using it as an int* can also lead to undefined behavior. My preference is to use packed structs only at the read/write boundary and copy the data into normal structs for everything else. Don't forget byte order (endianness) either; a uint32_t coming from the network usually needs ntohl.
Where this knowledge pays off
- In embedded systems, where every byte of memory counts.
- When parsing file formats and network protocols.
- In reverse engineering, when rebuilding a struct's fields from offsets in compiled code. When you see
[rdi + 0x8]in Ghidra, knowing where that 8 comes from changes everything. - In performance-critical code, when designing data structures that fit in cache.
Padding is a nice example of how closely C shows you the machine. The struct you write isn't the struct the CPU sees; the compiler fills the gap. If you're curious about the differences between the two architectures, have a look at my post comparing ARM and x86.
Related articles
- malloc and free in C: dynamic memory and how to find memory leaksC Programming · 4 min read
- Pointers and arrays in C: why does an array turn into a pointer?C Programming · 4 min read
- What is a segmentation fault and how do you find it? A step-by-step guide with GDB and AddressSanitizerC Programming · 5 min read