Manual allocation and deallocation
Quick answer
Manual allocation is a low-level tool for owning raw memory. The allocation layout, initialization state, ownership and deallocation layout must remain consistent.
Match allocation and cleanup
Layout specifies size and alignment. The example uses a nonzero layout, handles null allocation, writes a valid u64 and releases the allocation exactly once with the same layout. For types with destructors, drop the initialized object as required before releasing raw storage; plan for panic and partial initialization. Prefer Box or Vec for ordinary ownership and cleanup.
use std::alloc::Layout;fn main() { let l = Layout::new::<u64>(); println!("{}", l.size()); }Expected stdout (not executed):
8The size is eight bytes; alignment can differ by target.
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};fn main() { let l = Layout::new::<u64>(); // SAFETY: l has nonzero size and valid alignment. let p = unsafe { alloc(l) }.cast::<u64>(); if p.is_null() { handle_alloc_error(l); } // SAFETY: allocation is live, aligned, exclusive and large enough for u64. // write initializes it, read copies a u64, then matching dealloc frees it once. let value = unsafe { p.write(42); let v = p.read(); dealloc(p.cast(), l); v }; println!("{value}");}Expected stdout (not executed):
42No fallible user code runs between successful allocation and cleanup; u64 requires no destructor.
Common mistake
Never deallocate with an unrelated layout or reuse the pointer after freeing it.
Try it yourself
Rewrite the allocation using Box and identify the manual obligations that disappear.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Manual allocation and deallocation — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A raw allocation contains a live String. What must happen before deallocating its storage?
Manual allocation and deallocation — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::alloc::Layout; fn main() { let l = Layout::new::<u64>(); println!("{}", l.size()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Manual allocation and deallocation — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::alloc::{alloc, dealloc, handle_alloc_error, Layout}; fn main() { let l = Layout::new::<u64>(); // SAFETY: l has nonzero size and valid alignment. let p = unsafe { alloc(l) }.cast::<u64>(); if p.is_null() { handle_alloc_error(l); } // SAFETY: allocation is live, aligned, exclusive and large enough for u64. // write initializes it, read copies a u64, then matching dealloc frees it once. let value = unsafe { p.write(42); let v = p.read(); dealloc(p.cast(), l); v }; println!("{value}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.