Overview
TheAllocator interface is Zig’s standard abstraction for memory allocation. All allocators in Zig implement this common interface, providing a uniform API for memory management across different allocation strategies.
Structure
Type-erased pointer to the allocator implementation. May be set to
undefined when the allocator has no associated state.Pointer to the virtual function table containing the allocator’s implementation methods.
VTable Methods
TheVTable struct defines the core allocation operations:
alloc
Number of bytes to allocate. Must be greater than zero.
Required memory alignment.
Return address for debugging.
0 means no return address provided.Pointer to allocated memory, or
null if allocation failed.resize
Existing allocation slice. Length must equal the most recent allocation size.
Must match the alignment from the original allocation.
Desired new size. Must be greater than zero.
true if resize succeeded in place, false if relocation would be required.remap
Pointer to the resized memory (may be same address or relocated), or
null if the allocator cannot perform the operation efficiently.free
Memory to free. Length must match the most recent allocation size.
Must match the alignment from the original allocation.
High-Level API
create
T.
The type to allocate.
Pointer to uninitialized memory for the object.
destroy
create.
alloc
n items of type T.
Number of elements to allocate.
Slice of uninitialized memory.
allocSentinel
n + 1 items with a sentinel value at the end.
Example:
alignedAlloc
Desired alignment, or
null for natural alignment.realloc
Existing allocation. May be empty (length 0).
New element count. May be zero to free the allocation.
free
alloc, alignedAlloc, or realloc.
Freeing zero-length slices is a no-op.
dupe
dupeZ
Error Type
Error!T, where the only possible error is OutOfMemory.
Alignment
Alignment Methods
toByteUnits() - Converts to byte alignment
fromByteUnits(n: usize) - Creates from byte alignment
of(comptime T: type) - Gets natural alignment of type
check(address: usize) - Verifies address alignment
Common Allocators
page_allocator
Direct syscall-based allocator. Thread-safe but slow.c_allocator
Wraps C’smalloc/free. Only available when linking libc.
FixedBufferAllocator
Allocates from a fixed buffer. Fast but limited.Best Practices
Always free allocations
Always free allocations
Use
defer to ensure memory is freed:Match allocation and deallocation
Match allocation and deallocation
- Use
destroy()forcreate() - Use
free()foralloc(),realloc(),dupe() - Pass the same length and alignment used during allocation
Check for zero-sized allocations
Check for zero-sized allocations
Zero-length allocations are valid and return empty slices.
Prefer arena allocators for bulk operations
Prefer arena allocators for bulk operations
When making many allocations that have the same lifetime, use
ArenaAllocator to free them all at once.See Also
- ArenaAllocator - Bulk allocation and deallocation
- Heap Allocators - General-purpose allocators