liveBook · Manning

Raw pointer

As explained in Embedded Software with Rust

Raw pointer dereferencing accesses memory through a Rust raw pointer without the safety guarantees of an ordinary reference.

A raw pointer holds an address that Rust does not check like an ordinary reference. Creating one is allowed in safe code, but dereferencing it requires an unsafe block. The programmer must uphold the guarantees the compiler cannot verify; unsafe does not turn off the borrow checker or type system. section 5.2.2 section 7.1.3

In firmware, raw pointers give direct access to memory-mapped peripheral registers. Before dereferencing one, check that the address is valid, correctly aligned, accessible in the current privilege and security state, and represents the intended register type. section 5.2.2

const GPIOA_ODR: *mut u32 = 0x4001_080C as *mut u32;

// More code
unsafe {
    *GPIOA_ODR = 0x0000_0001;
}

This STM32F1 example casts the GPIO output register address to *mut u32 and writes through it. The address satisfies the pointer’s four-byte alignment requirement. section 5.2.2

A plain dereference is a useful starting point, but it is not a robust MMIO pattern: the compiler may remove, merge, or reorder accesses. Use read_volatile and write_volatile, or a register API that performs volatile access. A full-register write also overwrites every bit; use the hardware’s set/reset registers or a suitable read-modify-write pattern when other bits must be preserved. section 5.2.2 section 5.6

use core::ptr;

const SYST_CSR: *mut u32 = 0xE000_E010 as *mut u32; // Control & Status
const SYST_RVR: *mut u32 = 0xE000_E014 as *mut u32; // Reload
const SYST_CVR: *mut u32 = 0xE000_E018 as *mut u32; // Current

unsafe {
    // example: max reload (24-bit field)
    ptr::write_volatile(SYST_RVR, 0x00FF_FFFF);
    // writing any value to CVR clears the counter and COUNTFLAG
    ptr::write_volatile(SYST_CVR, 0);

    // ENABLE (bit0) | TICKINT (bit1) | CLKSOURCE=Core (bit2)
    ptr::write_volatile(SYST_CSR, 0x0000_0007);
}

The SysTick example uses volatile writes to set the reload value, clear the counter, and enable the timer and its interrupt. section 5.2.5

Keep raw pointer operations in small, audited APIs. Peripheral access crates (PACs) and hardware abstraction layers (HALs) can hide low-level operations behind more structured interfaces. section 5.2.1 section 5.6

Raw pointers also appear in manual allocation and Rust–C interfaces. A null check is necessary where null is possible, but a non-null pointer can still be dangling, misaligned, or unwritable. A pointer passed to C must remain valid for as long as C uses it. section 7.1.3 section 8.3.4 section 8.4.1

extern crate alloc;

use alloc::alloc::{alloc, dealloc, Layout};

fn manual_heap_example() {
    // Describe what we want: size and alignment of a u32.
    let layout = Layout::new::<u32>();

    unsafe {
        let ptr = alloc(layout);
        if ptr.is_null() {
            // Allocation failed; bail out before dereferencing.
            return;
        }

        // Write, then read back.
        *(ptr as *mut u32) = 1986;
        assert_eq!(*(ptr as *mut u32), 1986);

        // Deallocate using the same layout we allocated with.
        dealloc(ptr, layout);
    }
}

This allocation example checks for null before dereferencing and deallocates with the same layout used to allocate. Box handles these details automatically for ordinary owned values. section 7.1.3

How Embedded Software with Rust covers it

How deep it goes
Explains unsafe dereferencing, address and alignment checks, volatile access, and alternatives through worked code.
Examples
Uses STM32F1 GPIO and SysTick registers, manual heap allocation, and a C-facing output pointer.
What you can do after
Apply it to low-level register access while checking pointer obligations and choosing volatile operations for MMIO.
How it connects
Connects raw pointers to unsafe blocks, volatile access, memory-mapped I/O, peripheral access crates, and hardware abstraction layers.

Also explained in

Compare how 2 books explain raw pointer →

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.