**** Advance Notice ****

This site will be closed on 31 December 2015,

Important Information for users of NXP LPCXpresso

This site is for users of Code Red branded products.

NXP LPCXpresso users should visit the LPCXpresso FAQ's for up-to-date information relevant to that product.

Packed Structures

By default, GCC does not pack structures so it will in general leave spaces so that things get word aligned. This behavior can be changed by using the "packed" attribute on the structure.

struct foo
{  short a;
   char b;
   int c;
   char d;
} __attribute__((packed)); 

Some other toolchains have alternative ways of packing structures. For example a particular toolchain might use a qualifier "__packed" which needs to be placed before the structure rather than using an attribute after the structure. The following code snippet shows how code might be written to allow the code to be compilable using either toolchain...

#if defined(__GNUC__)   // Code Red tools
  #define PRE_PACK
  #define POST_PACK     __attribute__((packed))
#else                   // Other toolchain
  #define PRE_PACK      __packed
  #define POST_PACK

PRE_PACK struct foo
{  short a;
   char b;
   int c;
   char d;

Note that packing data can have code size and performance overheads. Thus it is not recommended for general use. However there are some circumstances where you may need to make use of it - for example for accessing data coming in from peripherals.

PackedStructs (last edited 2010-09-20 13:48:20 by CrSupportAb)