Skip to content

Array Alignment

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 AA whose element type requires alignment aa, ensure that every element address satisfies:

address(A[i])moda=0 address(A[i]) \bmod a = 0

for every valid index ii.

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 ss:

address(A[i])=base(A)+is address(A[i]) = base(A) + i \cdot s

The array is properly aligned when:

base(A)moda=0 base(A) \bmod a = 0

and:

smoda=0 s \bmod a = 0

Algorithm

Check element alignment:

is_aligned(address, alignment):
    return address mod alignment == 0

Check 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 true

Example

Suppose:

fieldvalue
base address1024
element size8
alignment8

Addresses are:

indexaddressaligned
01024yes
11032yes
21040yes
31048yes

Each address is divisible by 88, 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

operationtime
single address checkO(1)O(1)
full layout checkO(n)O(n)

In practice, full layout checking can often be reduced to constant time by verifying only:

base(A)moda=0 base(A) \bmod a = 0

and:

size(element)moda=0 size(element) \bmod a = 0

Space usage:

O(1) O(1)

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 True
func 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
}