malloc and free, you have three distinct mechanisms at your disposal: manual allocation with allocate and free, automatic deallocation with autoremove, and ownership transfer through Robbery. Together, they let you write systems code that is both precise and resistant to common memory bugs.
Stack vs Heap
Variables you declare normally live on the stack and are reclaimed automatically when their enclosing scope exits. To keep data alive beyond a scope — or to allocate large buffers — you put it on the heap using theallocate keyword.
| Location | How | Notes |
|---|---|---|
| Stack | Normal variable declaration | Freed automatically on scope exit |
| Heap | allocate keyword | Must be freed manually or via autoremove |
allocate — Heap Allocation
Use allocate to place a variable or array on the heap. There are two syntax forms depending on whether you want an array of elements or a single variable with a specific byte size.
| Syntax | Meaning |
|---|---|
allocate int buf[256] | Array of 256 int elements on the heap |
allocate int x(64) | Single variable allocated with a 64-byte backing size |
Custom byte sizes specified with the parenthesis form are bounds-checked at compile time. If the requested size is incompatible with the declared type, the compiler reports an error before generating any code.
free — Manual Deallocation
Call free to release the heap memory you allocated with allocate. The pointer is invalidated immediately after the call.
autoremove — Automatic Heap Deallocation
Prefix allocate with autoremove and the compiler’s simulation pass takes responsibility for freeing the memory. The pass performs static analysis to find the last use of the variable and inserts a free call at exactly that point during code generation. You may still free the pointer at any point, as long as it’s not being used before its last use.
free call placed at precisely the right line.
Robbery — Ownership Transfer
Robbery handles the case where anautoremove variable is about to be freed but another pointer still needs its memory. When a second pointer takes the address of an autoremove variable at the point the original would drop, the new pointer inherits ownership and becomes a plain heap variable — one that you manage manually or hand off to another autoremove binding.
Lifecycle Summary
The three memory flows map directly onto the three mechanisms:Rules & Gotchas
Common memory mistakes to avoid
Common memory mistakes to avoid
- Do not double-free a variable after it has already been freed
- Do not try to access an already freed portion of memory
- Avoid leaks in memory as much as possible and try to use robbery to aid in this
