liveBook · Manning

Alignment

As explained in Embedded Software with Rust

Alignment is the placement of a data object at an address satisfying the boundary requirements of its representation or hardware access.

Alignment is the requirement that a value be placed at an address on a specified byte boundary. Every Rust value has a size and an alignment: a u32, for example, has a size of 4 bytes and an alignment of 4 bytes. Alignment matters for efficient access and can prevent faults on hardware that does not permit misaligned accesses. section 5.4.1

A struct may need padding so its fields and the struct itself meet their alignment requirements. Its total size is rounded up to a multiple of its strictest field alignment. Use core::mem::size_of and align_of to inspect the resulting layout. section 5.4.1

use core::mem::{align_of, size_of};

struct SampleLayout(char, u16, u8);

fn main() {
    dbg!(size_of::<SampleLayout>());  // 8 bytes
    dbg!(align_of::<SampleLayout>()); // 4 bytes
}

This host-side example reports a size of 8 bytes and an alignment of 4 bytes for a struct whose fields total 7 bytes. section 5.4.1

Packing trades alignment for a smaller layout. With #[repr(C, packed)], fields follow C ordering but padding is removed. A wider field can then be at an address unsuitable for its natural alignment. On Cortex-M0 and Cortex-M0+, unaligned accesses trigger a HardFault; on Cortex-M3 and above, they incur a performance penalty. Avoid packed structs for memory-mapped I/O register overlays. section 5.4.3

use core::mem::{size_of, align_of};

/// A minimal two-field struct used to illustrate the effect of `#[repr(packed)]`.
/// This does not model any real hardware peripheral.
#[repr(C, packed)]
struct PackedExample {
    field16: u16,  // naturally 2-byte aligned
    field8: u8,    // naturally 1-byte aligned
}

fn main() {
    dbg!(size_of::<PackedExample>());  // 3 bytes: no padding inserted
    dbg!(align_of::<PackedExample>()); // 1 byte: the whole struct is byte-aligned
}

This host-side packed struct occupies 3 bytes and has an alignment of 1 byte, even though its u16 field is naturally 2-byte aligned. section 5.4.3

To require stricter alignment, use #[repr(align(N))]. The resulting size can include padding: a 32-byte buffer aligned to 64 bytes has a size of 64 bytes in the example below. section 5.4.3

use core::mem::{size_of, align_of};

#[repr(align(64))]
struct CacheAlignedBuffer([u8; 32]);

fn main() {
    dbg!(align_of::<CacheAlignedBuffer>()); // 64 bytes
    dbg!(size_of::<CacheAlignedBuffer>()); // 64 bytes (32 bytes payload + 32 bytes padding)
}

The host-side listing checks both the alignment and size of a 64-byte-aligned buffer. section 5.4.3

When a struct crosses a Rust–C boundary, both sides must agree on field order, alignment, and padding. Rust's default struct layout does not guarantee that agreement. Use #[repr(C)] on the Rust struct to match C layout; enums require additional care beyond #[repr(C)] alone. section 8.2.3

#[repr(C)]
pub struct SensorData {
    id: u8,
    value: u16,
    status: u8,
}

The C-compatible SensorData layout places its u16 field at offset 2 and occupies 6 bytes, including padding. section 8.2.3

How Embedded Software with Rust covers it

How deep it goes
Explains alignment, padding, packed-layout trade-offs, and explicit alignment through measured struct examples.
Examples
Uses host-side layout measurements for ordinary, packed, and explicitly aligned structs, plus a C-compatible sensor structure.
What you can do after
Evaluate struct alignment and padding with Rust's size_of and align_of, and choose representations for shared or hardware-facing data.
How it connects
Connects alignment to memory layout, padding, memory-mapped I/O, and foreign function interfaces.

Also explained in

Compare how 6 books explain alignment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.