CrossWorks offers packed structures, an extension to the ISO standard to pack fields in structure and union types. Packing a structure removes the gaps the compiler places between members to ensure that data are aligned correctly.


A packed structure is indicated by placing the keyword __packed before a structure declaration:

typedef __packed struct {
  char c;
  short s;
} packed_structure_t;

Without packing and for a processor that aligns short data on 16-bit boundaries, the compiler would leave one byte of unused space between the members c and s in order to correctly align s to the following 16-bit boundary. When the structure is packed, this gap is eliminated at the expense of additional code generated to load and store the packed members.


Packed structures are useful when you are directly mapping predefined communications layouts into structures. However, there are some restrictions on what you can do with packed structures: