For efficiency, fields in a structure are positioned on their
natural size boundary. This means that the compiler often inserts
padding between fields to ensure that they are naturally aligned.
When space is at a premium, the __packed qualifier
can be used to create structures without padding between fields.
Structures can be packed in the following ways:
The entire struct can
be declared as __packed. For example:
__packed struct mystruct
{
char c;
short s;
} // not recommended
Each field of the structure inherits the __packed qualifier.
Declaring an entire struct as __packed typically
incurs a penalty both in code size and performance.
Individual non-aligned fields within the struct can
be declared as __packed. For example:
This is the recommended approach to packing structures.
Note
The same principles apply to unions. You can declare either
an entire union as __packed, or use the __packed attribute
to identify components of the union that are unaligned in memory.
This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.
ARM websites use two types of cookie: (1) those that enable the site to function and perform as required; and (2) analytical cookies which anonymously track visitors only while using the site. If you are not happy with this use of these cookies please review our Privacy Policy to learn how they can be disabled. By disabling cookies some features of the site will not work.