Skip to main content

IAR Embedded Workbench for Arm 9.70.x

Alignment

In this section:

Every C data object has an alignment that controls how the object can be stored in memory. Should an object have an alignment of, for example, 4, it must be stored on an address that is divisible by 4.

The reason for the concept of alignment is that some processors have hardware limitations for how the memory can be accessed.

Assume that a processor can read 4 bytes of memory using one instruction, but only when the memory read is placed on an address divisible by 4. Then, 4-byte objects, such as long integers, will have alignment 4.

Another processor might only be able to read 2 bytes at a time—in that environment, the alignment for a 4-byte long integer might be 2.

A structure type will have the same alignment as the structure member with the strictest alignment. To decrease the alignment requirements on the structure and its members, use #pragma pack or the __packed data type attribute.

All data types must have a size that is a multiple of their alignment. Otherwise, only the first element of an array would be guaranteed to be placed in accordance with the alignment requirements. This means that the compiler might add pad bytes at the end of the structure. For more information about pad bytes, see Packed structure types.

Note

With the #pragma data_alignment directive, you can increase the alignment demands on specific variables.

See also the Standard C file stdalign.h.

Alignment on the Arm core

The alignment of a data object controls how it can be stored in memory. The reason for using alignment is that the Arm core can access 4-byte objects more efficiently when the object is stored at an address divisible by 4.

Objects with alignment 4 must be stored at an address divisible by 4, while objects with alignment 2 must be stored at addresses divisible by 2.

The compiler ensures this by assigning an alignment to every data type, ensuring that the Arm core will be able to read the data.

For related information, see ‑‑align_sp_on_irq and ‑‑no_const_align.