Allocation

Stack allocation

Local variables, fixed-size arrays, and array literals live on the stack:

tape
fn compute() {
    var buffer: [1024]u8;              // stack-allocated
    let items = [1, 2, 3, 4, 5];      // stack-allocated
    var point: Vec2;                   // stack-allocated (uninitialized struct)
}

Stack space is reserved in the function prologue. No allocation call is needed.

Heap allocation

Struct literals and component instances are heap-allocated:

tape
let p = Person { name: "Alice"; age: 30; };  // heap-allocated
let app = Counter {};                         // heap-allocated (component)

Struct literals use the heap so the value can survive returns and be passed by pointer.

The mem module

For manual heap allocation, import the mem module:

tape
import mem from "mem";

let data: *u8 = mem.alloc(size);
defer mem.free(data, size);
FunctionSignatureDescription
alloc(size: u64) -> *u8Allocate bytes (zeroed)
free(ptr: *u8, size: u64)Free allocation
copy(dst: *u8, src: *u8, count: u64)Copy bytes (non-overlapping)
move(dst: *u8, src: *u8, count: u64)Copy bytes (overlapping safe)
set(dst: *u8, val: u8, count: u64)Fill memory with byte value
zero(dst: *u8, count: u64)Zero-fill memory
compare(a: *u8, b: *u8, count: u64) -> i32Compare bytes (-1, 0, 1)
equal(a: *u8, b: *u8, count: u64) -> boolByte equality check

Note: mem.free takes both the pointer and the size. The runtime does not track allocation sizes.

String allocation

The string type is a 24-byte descriptor (pointer + length + capacity). String operations that produce new values (+, interpolation) heap-allocate internally. The compiler manages string lifetime automatically — see Ownership.

tape
let greeting: string = "Hello, " + name;  // heap-allocated result
// freed automatically at scope exit

Where values live

ValueLocation
Primitives (i64, bool, f64, etc.)Stack (local slot)
Fixed arrays ([N]T)Stack
Array literals ([1, 2, 3])Stack
Uninitialized struct localsStack
Struct literals (Type { ... })Heap
Component instances (Comp {})Heap
Strings (dynamic)Heap (descriptor on stack, data on heap)
mem.alloc(...)Heap
SlicesStack (fat pointer: ptr + len)

Last modified: