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);| Function | Signature | Description |
|---|---|---|
alloc | (size: u64) -> *u8 | Allocate 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) -> i32 | Compare bytes (-1, 0, 1) |
equal | (a: *u8, b: *u8, count: u64) -> bool | Byte 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 exitWhere values live
| Value | Location |
|---|---|
Primitives (i64, bool, f64, etc.) | Stack (local slot) |
Fixed arrays ([N]T) | Stack |
Array literals ([1, 2, 3]) | Stack |
| Uninitialized struct locals | Stack |
Struct literals (Type { ... }) | Heap |
Component instances (Comp {}) | Heap |
| Strings (dynamic) | Heap (descriptor on stack, data on heap) |
mem.alloc(...) | Heap |
| Slices | Stack (fat pointer: ptr + len) |
Last modified: