Structure Padding

This tip has been extracted from the main structure padding article.

Compilers might shift the offset of structure members so that they are aligned on device-specific memory boundaries and can be easily accessed. This padding forms part of the structure and can result in the size of a structure being larger than the sum of the sizes of its members.

You should never make assumptions about the size of structures, and you should always use the sizeof() operator to obtain a structure’s size. Do not assume that a structure member follows immediately in memory after the previous member. Always use the address operator, &, to obtain the address of a structure member. Structure padding implies that you should be careful using a char pointer to access each byte of a structure, since some of those bytes might be padding and hold meaningless values.

Some compilers might pack structure members on an alignment which is equal to the size of that member, so the ordering of the members can affect the structure size. Placing members with the same size sequentially might result in smaller-sized structures.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.