| |||
| Home > Coding Practices > Aligning data | |||
The various C data types are aligned on specific byte boundaries to maximize storage potential and to provide for fast, efficient memory access with the ARM instruction set. For example, the ARM architecture can access a four-byte variable using only one instruction when the object is stored at an address divisible by four, so four-byte objects are located on four-byte boundaries.
By default, the compiler stores data objects as shown in Table 5.9.
Table 5.9. Compiler storage of data objects by byte alignment
| Type | Bytes | Alignment |
|---|---|---|
char | 1 | Located at any byte address. |
short | 2 | Located at any address that is evenly divisible by 2. |
float, int, long, pointer | 4 | Located at an address that is evenly divisible by 4. |
long long
double | 8 | Located at an address that is evenly divisible by 4. |
Data alignment becomes relevant when the compiler locates
variables to physical memory addresses. For example, in the following
structure, a three-byte gap is required between bmem and cmem.
struct example_st {
int amem;
char bmem;
int cmem;
};
ARM and Thumb processors are designed to efficiently access naturally-aligned data, that is, doublewords that lie on addresses that are multiples of four, words that lie on addresses that are multiples of four, halfwords that lie on addresses that are multiples of two, and single bytes that lie at any byte address. Such data is located on its natural size boundary.