What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Data structure alignment is the placement of data at memory addresses that satisfy the alignment requirements of its types and platform. In C and C++, a compiler may insert padding between structure members or at the end of a structure so fields and array elements start at suitable boundaries. The exact offsets and size depend on the compiler, target architecture, and ABI—not just the member types.
What does alignment mean?
A type’s alignment requirement constrains which memory addresses are suitable for its objects. One way to describe an address’s alignment is by its remainder when divided by a power of two: an address aligned to a particular boundary has the required remainder for that boundary. The relevant requirements vary by type and implementation. Microsoft’s C++ alignment documentation explains alignment and illustrates how it affects structure layout.
Alignment is a rule about placement, not extra data stored in a field. When adjacent members cannot both begin at suitable addresses without a gap, the compiler can use padding bytes to make the layout work.
Recommended Free Tools
Why does a structure have padding?
Consider Microsoft’s documented example:
struct x_ {
char a;
int b;
short c;
char d;
};
In the documented compiler environment, a is 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 structure’s total size to 12 bytes. These offsets and this size illustrate the mechanism; they are not guaranteed for every compiler, target, or ABI.
#1 Best Overall
Internal padding
Internal padding is a gap between members. In the example, the gap before b lets that member begin at an address suitable for its alignment requirement.
Tail padding
Tail padding is unused space after the final member. A structure’s overall alignment can require its size to be rounded up. That matters in an array: each next element begins after the previous element’s full sizeof size, so tail padding can help keep each element aligned.
Rank #2
Why can sizeof exceed the sum of the fields?
sizeof includes padding as well as the storage occupied by members. A structure’s total size may therefore exceed the simple sum of its visible fields: internal gaps separate members, and tail padding may round the total size to a suitable boundary. In Microsoft’s particular example, the fields occupy 8 bytes in total, while the structure size is 12 bytes because of padding.
Do not treat those numbers as universal. The same declarations can have different offsets or total sizes under another compiler, target, or ABI. NVIDIA’s HPC Compilers Reference Guide 25.5 likewise cautions against assuming a fixed layout for complex C++ classes, including classes with inheritance or virtual functions.
How can you inspect or request alignment?
In C++, alignof(T) queries a type’s alignment requirement, and alignas requests an alignment for a declaration. Microsoft documents both in its alignment reference. To check a structure’s actual layout, inspect its sizeof, alignof, and member offsets in the build configuration that matters. Treat those results as facts about that compiler and target, not as portable constants.
Can changing field order reduce padding?
Sometimes. Placing members with larger alignment requirements before those with smaller requirements can reduce gaps in a particular layout. But the result depends on the target’s type widths and ABI, and a smaller structure is not automatically faster. Apple’s archived Performance Optimization guide illustrates field ordering and notes how data-model changes between 32-bit and 64-bit builds affect results.
Rank #4
- Check offsets and size on each target build you support.
- Measure performance on the actual workload and processor rather than assuming a compact arrangement is faster.
- Keep external representation requirements separate from internal memory optimization.
What does packing change, and when is it appropriate?
Packing directives or compiler options can alter member layout to reduce padding, but their behavior is compiler- and platform-specific. Packed members may be misaligned; on some processors, accessing them can reduce performance or cause faults. Intel’s Vectorization Programming Guidelines discuss alignment in the context of performance, while Microsoft’s Windows driver porting checklist warns about layout and misaligned pointer fields.
Use packing when an external specification or hardware interface requires a particular representation, and verify the compiler-specific behavior and access requirements. It is not a general-purpose way to make structures better: it trades padding for possible access, performance, and portability problems. Apple’s archived guide also advises against routine packing.
Best Value
Why should binary formats avoid relying on an accidental struct layout?
A structure’s in-memory bytes are not automatically a portable file format, network message, hardware register map, or cross-process interface. Different compilers, ABIs, targets, and pointer widths can produce different layouts. For an external binary representation, define the byte order, field widths, and offsets explicitly and serialize to that specification. If an interface intentionally uses a platform ABI, follow that ABI and account for differences such as 32-bit versus 64-bit processes; Microsoft highlights these concerns for data exchanged with 32-bit Windows processes.
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.

