Place array elements at memory addresses that satisfy hardware and type alignment requirements.
Array alignment controls where elements begin in memory. Many processors access aligned values more efficiently because the value fits cleanly inside one memory word, cache line, or vector register.
You use it when low-level performance, SIMD instructions, or binary layout compatibility matters.
Problem
Given an array whose element type requires alignment , ensure that every element address satisfies:
for every valid index .
Structure
If the base address is aligned and the element size is a multiple of the alignment, then every element is aligned.
For an array with element size :
The array is properly aligned when:
and:
Algorithm
Check element alignment:
is_aligned(address, alignment):
return address mod alignment == 0Check array layout:
array_is_aligned(base, element_size, alignment, n):
if base mod alignment != 0:
return false
for i from 0 to n - 1:
address = base + i * element_size
if address mod alignment != 0:
return false
return trueExample
Suppose:
| field | value |
|---|---|
| base address | 1024 |
| element size | 8 |
| alignment | 8 |
Addresses are:
| index | address | aligned |
|---|---|---|
| 0 | 1024 | yes |
| 1 | 1032 | yes |
| 2 | 1040 | yes |
| 3 | 1048 | yes |
Each address is divisible by , so each element is aligned.
Correctness
The algorithm tests the base address first. If the base is misaligned, the first element is misaligned. For each element, it computes the address using the standard array layout formula and checks divisibility by the required alignment.
Therefore, it returns true exactly when every element address satisfies the alignment condition.
Complexity
| operation | time |
|---|---|
| single address check | |
| full layout check |
In practice, full layout checking can often be reduced to constant time by verifying only:
and:
Space usage:
When to Use
Array alignment is important when:
- using SIMD or vector instructions
- reading or writing binary formats
- sharing memory with hardware devices
- optimizing cache-line and word-level access
- implementing allocators or storage engines
It is less visible in high-level code, where runtimes usually handle alignment automatically.
Implementation
def is_aligned(address, alignment):
if alignment <= 0:
raise ValueError("alignment must be positive")
return address % alignment == 0
def array_is_aligned(base, element_size, alignment, n):
if alignment <= 0:
raise ValueError("alignment must be positive")
if base % alignment != 0:
return False
for i in range(n):
address = base + i * element_size
if address % alignment != 0:
return False
return Truefunc IsAligned(address, alignment uintptr) bool {
if alignment == 0 {
return false
}
return address%alignment == 0
}
func ArrayIsAligned(base, elementSize, alignment uintptr, n int) bool {
if alignment == 0 {
return false
}
if base%alignment != 0 {
return false
}
for i := 0; i < n; i++ {
address := base + uintptr(i)*elementSize
if address%alignment != 0 {
return false
}
}
return true
}