Alignment
For circuitry reasons, CPUs access primitive values at specific multiples in
memory. This could mean, for example, that the address of an f32
value must be
a multiple of 4, meaning f32
has an alignment of 4. This so-called “natural
alignment” of primitive data types depends on CPU architecture. All alignments
are powers of 2.
Data of a larger alignment also has the alignment of every smaller alignment; for example, a value which has an alignment of 16 also has an alignment of 8, 4, 2 and 1.
We can make specially aligned data by using the align(x)
property. Here we are
making data with a greater alignment.
And making data with a lesser alignment. Note: Creating data of a lesser alignment isn’t particularly useful.
Like const
, align
is also a property of pointers.
Let’s make use of a function expecting an aligned pointer.