allocate for heap storage and free for manual deallocation.
Stack
Local variables follow C scope and lifetime rules.
Heap
Use
allocate for dynamic storage and free when you are done.Stack and heap
Ordinary local variables use the C stack model and have automatic scope lifetime. Useallocate when you need heap storage.
Allocate heap memory
Use brackets to allocate an array of elements:Release allocations
Callfree when the allocation is no longer needed:
free more than once for the same allocation.
1
Allocate
Choose an element-counted array or a custom byte size.
2
Use
Read and write the allocation while it is still valid.
3
Free
Release it exactly once with
free.Unsupported automatic ownership features
The language design includesautoremove for automatic deallocation and Robbery for ownership transfer. Neither feature is implemented in the current compiler. Use explicit free calls instead.