← All posts
November 22, 2026 · 3 min read

Struct padding in C++: alignment and why sizeof is bigger

Struct padding in C++, byte by byte: char, int, char holds 6 bytes of data but sizeof is 12. Put the int first and the same members fit in 8.

Struct padding in C++ is why sizeof of a struct is often bigger than the sum of its members. struct A { char c; int i; char d; }; holds 1 + 4 + 1 = 6 bytes of data, yet sizeof(A) is 12. The other 6 bytes are padding, and where they go follows from one rule: alignment.

This is the written version of Understanding C++ #10. Every number below is printed by one program, compiled with g++ -std=c++20 -Wall -O2 (GCC 13) on x86-64 Linux, where char is 1 byte and int is 4 bytes with an alignment of 4. Other platforms can differ; the program tells you what yours does.

Six bytes of data, yet sizeof(A) = 12: twelve empty byte cells for struct A

What is alignment?

alignof(int) prints 4. An int must sit at an address that is a multiple of 4. A struct's own alignment is the largest alignment of its members, so an A also starts at a multiple of 4, and inside it each member's offset must be a multiple of that member's alignment. offsetof(A, member) gives the offset in bytes from the start of the struct.

Where do the padding bytes go in struct A?

c is a char (alignment 1), so it takes offset 0. The next free offset is 1, but i needs a multiple of 4, so the compiler inserts three padding bytes at offsets 1, 2 and 3 and puts i at 4 to 7. d lands at 8.

alignof(int) = 4: offsets 0, 4 and 8 marked; c at offset 0, then three padding bytes at 1, 2 and 3

That makes 9 bytes, but sizeof(A) is 12. Three more padding bytes at the end, offsets 9 to 11, round the size up to a multiple of the struct's alignment, 4. The reason is arrays: the elements of A arr[2] sit back to back, sizeof(A) apart. With a size of 12, arr[1] starts at offset 12 and its i at offset 16, a multiple of 4. With a size of 9, it would sit at offset 13.

struct A in 12 bytes: c at 0, padding 1 to 3, i at 4 to 7, d at 8, padding 9 to 11; sizeof(A) = 12 = 3 x 4

offset 0 1 2 3 4 5 6 7 8 9 10 11
struct A c pad pad pad i i i i d pad pad pad

The values in padding bytes are unspecified, so you should never read them. To see the layout in real memory, a second check program fills an A with the byte EE, sets c = 'x', i = 7 and d = 'y', and dumps it lowest address first: 78 ee ee ee 07 00 00 00 79 ee ee ee. The x (78), the little-endian 7 and the y (79) sit exactly at offsets 0, 4 and 8; the ee bytes are the padding.

How does reordering the members save memory?

struct B { int i; char c; char d; }; has the same three members, with the int first. i takes offsets 0 to 3, c takes 4 and d takes 5, since a char can go anywhere. Two padding bytes at 6 and 7 round the size up to a multiple of 4, and sizeof(B) is 8.

struct B in 8 bytes: i at 0 to 3, c at 4, d at 5, padding at 6 and 7; sizeof(B) = 8

offset 0 1 2 3 4 5 6 7
struct B i i i i c d pad pad

Same members, four bytes saved per object. Ordering members from largest alignment to smallest usually removes most of the padding, because each member then starts where the previous one ended. GCC can show you where padding goes with -Wpadded; on this program it warns about the 3 bytes before A::i, the 3 bytes at the end of A and the 2 at the end of B.

12 minus 8 is 4 bytes saved, with the whole program layout.cpp below

The program and its output

#include <cstddef>
#include <cstdio>
struct A { char c; int i; char d; };
struct B { int i; char c; char d; };
int main() {
    std::printf("%zu\n", alignof(int));
    std::printf("%zu %zu %zu\n", offsetof(A, c),
                offsetof(A, i), offsetof(A, d));
    std::printf("%zu\n", sizeof(A));
    std::printf("%zu %zu %zu\n", offsetof(B, i),
                offsetof(B, c), offsetof(B, d));
    std::printf("%zu\n", sizeof(B));
}
4
0 4 8
12
0 4 5
8

Line by line: 4 is alignof(int); 0 4 8 are the offsets of c, i and d in A; 12 is sizeof(A); 0 4 5 are the offsets of i, c and d in B; 8 is sizeof(B).

Try it

A double, a char, then an int: what is the sizeof of that struct? On x86-64 a double is 8 bytes with an alignment of 8. Work out each offset and the padding at the end, then check it with offsetof and sizeof.