Enforcing packing of variables in GCC/ARM compiler

GCC and ARM compilers do not recognize the #pragma pack directive.

[size=150]Directives to use for packing and alignment using ARM compiler[/size]

  1. To align all the fields of structure/union on single byte boundary (no padding), use __packed qualifier. For example,

This qualifier indicates that:

  • There is no padding inserted to align the packed objects.
  • Objects of packed type are read or written using unaligned accesses.


  • __packed qualifier cannot be used for floating-point types.
  • It cannot be used for structures or unions with floating-point fields.
  • It cannot be used for structures that were previously declared without __packet.
  • The __packed qualifier applies to all the members of a union or structure when it is declared using __packed. There is no padding between members, or at the end of the structure. All the substructures of a packed structure must be declared using __packed.

Integral subfields of an unpacked structure can be packed individually. However, access to packed structure considerably reduces the execution speed. This qualifier is equivalent to #pragma pack(1).

  1. To align objects on boundaries which fall on power of 2 (like 2, 4, 8 etc), the __align(n) class modifier can be used. For example,

But this modifier can only be used for top level objects. For example, if there are definitions like:

Then the structure test1 will start at a 4 byte aligned boundary. This alignment will be relative to the previous variable (i.e. u16 integer).
If integer starts at memory address 17169148 then the structure test will start at memory address 17169852. This modifier cannot be used to align the members of the structure/union.
To have the elements of a structure aligned at specified boundaries, the padding should be done manually.

For example,

In the above structure, the first_field will always start at four byte aligned boundary, but as __align modifier will not provide alignment for the members of the structure, the padding is provided manually.

[size=150]Directives to use for alignment and packing in GCC compiler:[/size]

  1. To align all the fields of a structure/union on a single byte boundary (no padding), use the keyword attribute with packed option.

    For instance,

The packed option specifies that each member of the structure/union should be placed to minimize the memory requirement. This is equivalent to #pragma pack (1).

  1. To align members of structure/union on boundaries which fall on the power of 2 (like 2, 4, 8, 16) use the option aligned(n) with attribute keyword.

For instance:

In the above definition of structure test, each member will be aligned on a 4 byte boundary.
Please note that it is up to the linker if it can provide the facility of aligning the objects on higher alignment values (like 16,32,64).

If this options is provided on top level object definition like:

        typedef struct
                    u8 first;
                    u16 second;
                    u16 third;
                    float fourth;
                    s8 fifth;
        } __attribute__ ((aligned (4))) test;

then the structure (test) itself will be aligned at 4 byte boundary. The elements which comprise the structure will occupy the storage according to the default storage option.