Memory alignment means that a C++ object of a given type must be stored at an address that is a multiple of that type's alignment requirement (for example 4 for a typical int, 8 for a double). To satisfy this, the compiler inserts unused bytes called padding between struct members and at the end of the struct. As a result, sizeof a struct is often larger than the sum of its members, and reordering members can shrink it.
This guide covers struct padding alignment c++ rules: how the compiler lays out members, how to inspect it with sizeof, alignof and offsetof, and when to reorder, pack or align on purpose. The numbers shown are for a common 64-bit platform (x86-64 Linux, macOS or Windows); other targets can differ.
CPUs read memory most efficiently in aligned chunks. Reading a 4-byte int from an address that is not a multiple of 4 may take two memory accesses, cross a cache line or page boundary, or fault outright on some architectures. SIMD instructions and atomic operations often require stricter alignment still. So the compiler gives every type an alignment, available as alignof(T), and places objects only at suitable addresses.
For fundamental types on a typical 64-bit platform:
| Type | sizeof | alignof |
|---|---|---|
char | 1 | 1 |
short | 2 | 2 |
int, float | 4 | 4 |
double, long long, pointers | 8 | 8 |
(long double and long vary by platform.) The size of a type is always a multiple of its alignment, which is what makes arrays of that type work.
The rules are simple:
Both structs hold the same three members, yet one is a third bigger. The general rule for saving space is to order members from largest alignment to smallest.
In A, d must start at offset 8, so 7 bytes of padding follow c1, and c2 needs 7 bytes of tail padding so the next array element's d is aligned. In B only 6 bytes of tail padding remain. For millions of objects in an array or vector, this is a real memory and cache saving.
Use these tools instead of guessing:
sizeof(T) for the total size and alignof(T) for the alignment.offsetof(T, member) from cstddef, which is fully defined for standard-layout types.static_assert(sizeof(T) == N) to freeze a layout you depend on.-Wpadded, and Clang has -Wpadded, which list where padding is inserted. They are noisy, so use them on specific headers.pahole from the dwarves package shows holes in compiled structs on Linux.C++11 added alignas to request stricter alignment, which is useful for SIMD data or to avoid false sharing between threads.
Two threads updating different Counter objects no longer fight over one cache line. C++17 also lets new honour over-aligned types, with std::aligned_alloc or std::align_val_t available for manual allocation.
To remove padding you can pack a struct, but this is not standard C++. GCC and Clang use __attribute__((packed)) and MSVC uses #pragma pack(1).
Packed members can be unaligned, which is slower, can trap on some CPUs, and prevents taking a normal reference to a member. Use packing only for file formats and wire protocols, and prefer to serialise fields explicitly (with memcpy into a byte buffer) and handle endianness yourself. Never send raw padded structs across a network or between machines.
[[no_unique_address]] does the same for an empty data member.memcmp or hash them as raw bytes. Compare member by member, or use a defaulted operator== (C++20).std::vector<bool>-style tricks and std::bitset trade speed for size when you need dense flags.Optimising layout is worthwhile for hot data structures. For most code, readability of member order matters more, so measure before reordering.
The compiler added padding bytes between members and at the end so every member and every array element is correctly aligned.
Yes, sort them by decreasing alignment. C++ guarantees members are laid out in declaration order (within the same access level), so the compiler will not do it for you.
No. The standard does not fix sizes or alignments. Platform ABIs define them, and 32-bit and 64-bit builds can differ.
new returns memory suitably aligned for any ordinary type. Over-aligned types need C++17 aligned new or alignas support from your allocator.
Alignment makes each object start at an address that is a multiple of its alignment, and the compiler inserts padding between members and at the end of structs to guarantee it. Check layouts with sizeof, alignof and offsetof, order members from largest to smallest alignment to save space, and use alignas to control cache-line behaviour. Reserve packing for external formats and never rely on padding bytes' contents.