Enhancing performance with packed structures and unions

Data elements of a structure or union are stored in memory on an address boundary specific for that data type. For example, a double value is stored in memory on a doubleword (8-byte) boundary. Gaps can be left in memory between elements of a structure to align elements on their natural boundaries. You can reduce the padding of bytes within a structure by packing that structure with the _Packed qualifier in C or by using the #pragma pack(packed) directive in C++ prior to the structure declaration.

The memory saved using packed structures might affect runtime performance. Most CPUs access data much more efficiently if it is aligned on appropriate boundaries. With packed structures, members are generally not aligned on appropriate (halfword, fullword, or doubleword) boundaries; the result is that member-accessing operations (. and ->) might be slower. The _Packed qualifier in C and #pragma pack(packed) in C++ have the same alignment rules for the same structures or unions. _Packed affects the definition, and #pragma pack(packed) affects the declaration.