Why Zig Has No Hidden Allocations
Allocation is always passed in.
Why Zig Has No Hidden Allocations is a free Zig Academy lesson on CoddyKit — lesson 1 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.
Memory Is Explicit
In Zig, no standard library function quietly grabs heap memory. If code needs to allocate, it must say so out loud. 📦
You Pass the Allocator
Anything that allocates takes an allocator as a parameter. Memory cannot appear unless you hand a function the tool to make it.
fn buildList(allocator: std.mem.Allocator) !void {}No Hidden new Keyword
Zig has no new operator and no garbage collector. Nothing reaches into the heap behind your back the way many languages do.
Why This Matters
Because allocation is visible, you always know where memory comes from. That is vital for embedded chips, kernels, and tight game loops.
Read the Signature
A function that may allocate shows it in its signature: it takes an allocator and usually returns an error union with !.
fn dupe(a: std.mem.Allocator, s: []const u8) ![]u8 {}The Stack Is Free
Plain locals live on the stack and need no allocator. You only reach for one when a value must outlive its function or grow at run time.
var x: i32 = 5; // stack, no allocationAllocation Can Fail
Asking for heap memory can fail, so allocating functions return an error. You must handle a possible OutOfMemory, never ignore it.
You Own What You Make
Whoever calls the allocator owns the result and must free it. Zig never frees your memory for you, so ownership is always clear.
Swap Allocators Freely
Because the allocator is a parameter, you can swap strategies without changing logic. Tests use one allocator, production another.
Libraries Stay Honest
A well-written library never hides an allocation. It accepts your allocator, so you control memory even inside other people is code.
Predictable by Design
This rule is part of Zig is promise of no hidden control flow. Explicit allocation keeps programs predictable and easy to reason about. ✅
Quick Check
Recall how a Zig function gets the power to allocate.
Recap
You saw that Zig has no hidden allocations: memory comes only through an allocator you pass in, and you own and free whatever you make. 🎯
Frequently asked questions
Is the “Why Zig Has No Hidden Allocations” lesson free?
Yes — the full text of “Why Zig Has No Hidden Allocations” 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 “Why Zig Has No Hidden Allocations”?
Allocation is always passed in. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Why Zig Has No Hidden Allocations” 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