The Allocator Interface
One contract for all memory sources.
The Allocator Interface is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Common Type
Every allocator in Zig is handed around as a single type, std.mem.Allocator. Your code talks to that one interface, never a concrete backend.
const Allocator = std.mem.Allocator;An Interface, Not a Class
std.mem.Allocator is a small interface: a pointer plus a table of functions. It describes what an allocator can do, not which one you use.
Many Backends, One Shape
A page allocator, an arena, or a test allocator all expose the same shape. Swapping one for another needs no change to calling code.
Get a Real Allocator
You obtain a concrete allocator, then take its .allocator() handle to pass around as the generic std.mem.Allocator type.
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const a = gpa.allocator();Pass It Down
Once you hold an allocator, you pass that one value into any function that needs memory. The whole program can share a single source.
try doWork(a);The Page Allocator
std.heap.page_allocator asks the OS directly for memory. It is simple but coarse, so it suits big or long-lived blocks, not tiny ones.
const a = std.heap.page_allocator;GeneralPurposeAllocator
The GeneralPurposeAllocator is the everyday choice in debug builds. It tracks every block and reports leaks and double-frees for you.
Why an Interface Helps
Coding against the interface means your function works with any allocator. The same code runs in tests, on bare metal, or in a server.
Functions Just Ask
A function does not care which allocator it got. It simply calls methods like alloc and free on the std.mem.Allocator it was given.
const buf = try a.alloc(u8, 16);No Global Allocator
Zig has no single global heap. You decide which allocator each part of your program uses, keeping memory sources visible and local.
Test with a Different One
In tests you pass std.testing.allocator instead. Same code, but now leaks fail the test, proving the interface pays off. ✅
Quick Check
Think about what type all Zig allocators share.
Recap
You learned that std.mem.Allocator is one interface over many backends, so code that asks for memory works with any allocator you supply. 🎯
Frequently asked questions
Is the “The Allocator Interface” lesson free?
Yes — the full text of “The Allocator Interface” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Allocator Interface”?
One contract for all memory sources. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Allocator Interface” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Zig Has No Hidden Allocations
- The Allocator Interface
- alloc, free, create, destroy
- Dynamic Lists with ArrayList