Data structure alignment is the rule that places an object at a memory address suitable for its type. In C and C++, a compiler may add padding between structure members or at the end so members and successive structure elements meet alignment requirements. That is why sizeof can be larger than the sum of a structure’s fields.
What alignment means
An object is aligned when its address meets a required boundary. One way to describe that boundary is that the address has the required remainder when divided by a power of two. A type’s alignment requirement constrains where an object of that type may begin; it does not mean that every type uses the same boundary.
As an Amazon Associate I earn from qualifying purchases.
Structure members are laid out in declaration order, but the compiler may leave unused bytes before a member if the next available address does not meet that member’s alignment requirement. Those gaps are called internal padding. The complete structure also has an alignment requirement, so its size may be rounded up with tail padding. Tail padding helps ensure that each element in an array of structures starts at a suitable address.
Why a structure can be larger than its fields
Consider Microsoft Learn’s documented C++ example:
#1 Best Overall
struct x_ {
char a;
int b;
short c;
char d;
};
In that documented compiler context, a starts at offset 0. Three padding bytes place b at offset 4; c follows at offset 8, and d is at offset 10. One tail-padding byte brings the total size to 12 bytes. This is an illustration of how padding works, not a portable promise about every compiler or target. Microsoft Learn: Alignment
The useful distinction is between the sizes of the named fields and the size of the complete object. Padding occupies space in the object representation even though it is not a declared member.
What determines the actual layout
The general alignment concept is consistent, but exact offsets, alignment values, and sizeof results depend on the compiler, target platform, and application binary interface (ABI). Data-model differences, such as pointer widths across 32-bit and 64-bit builds, can also change layouts. Do not infer a universal layout from one compiler’s example or assume two independently built programs will produce identical structure bytes. NVIDIA HPC Compilers Reference Guide 25.5
C++ classes with inheritance or virtual functions involve additional implementation-dependent layout rules. Their representation should not be inferred from the simpler rules illustrated by a structure containing only ordinary data members. NVIDIA HPC Compilers Reference Guide 25.5
How alignment controls differ from packing
In C++, alignof queries a type’s alignment requirement, while alignas requests an alignment for a declaration. Packing controls, by contrast, change how a compiler lays out members, often reducing or removing gaps. Their syntax and behavior are compiler- and platform-specific; consult the documentation for the toolchain you build with. Microsoft Learn: Alignment
Packing is not automatically an improvement. It may reduce an object’s size, but fields can then sit at addresses that are not naturally aligned. Depending on the processor and access, misaligned operations may be slower or may fault. Intel’s guidance discusses alignment in the context of vectorization, while Microsoft’s Windows driver guidance warns about risks such as misaligned pointer fields. Intel Vectorization Programming Guidelines; Microsoft Learn: Porting Issues Checklist — Windows drivers
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.How to inspect and reduce padding safely
Inspect the target build
When layout or memory use matters, check sizeof(T), alignof(T), and the offsets of the members in the actual compiler-and-target build you ship. These checks answer what that build produced; they do not establish a layout guarantee for a different ABI or compiler.
Consider member order, then measure
Putting members with larger alignment requirements before those with smaller ones can reduce padding in some layouts. It is not a universal rule for performance, and changes in data-model widths can affect the result between builds. Verify the resulting offsets and size, then measure performance on the target rather than assuming a smaller structure is faster. Apple’s archived optimization guide illustrates both field reordering and cross-build layout differences. Apple Developer Documentation: Performance Optimization
Best Value
Use packing only for a defined representation requirement
If a hardware interface or an external binary format requires a specific layout, follow its documented offsets and representation. Packing may be appropriate when supported by the compiler and target, but verify access safety and compiler behavior. For files, network messages, and data shared across processes, a serialization format with explicitly defined fields is generally safer than relying on a compiler’s incidental in-memory structure layout. Microsoft specifically calls for care when data is exchanged with 32-bit processes or structures contain pointers. Microsoft Learn: Porting Issues Checklist — Windows drivers; Apple Developer Documentation: Performance Optimization
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




