Skip to main content

IAR Embedded Workbench for RX 5.20

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.