alloc, free, create, destroy
The core allocator operations.
alloc, free, create, destroy is a free Zig Academy lesson on CoddyKit — lesson 3 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.
Four Core Operations
An allocator gives you four key calls: alloc and free for many items, create and destroy for a single value.
alloc Returns a Slice
Use alloc to request a run of items. You pass the element type and a count, and you get back a slice of that many elements.
const buf = try a.alloc(u8, 64);alloc Can Fail
Because the heap can run out, alloc returns an error union. The try keyword handles the OutOfMemory case for you cleanly.
const list = try a.alloc(i32, 10);free Returns It
When you are done, call free with the exact slice you got. Forget this and the GeneralPurposeAllocator will report a leak.
a.free(buf);defer free Right Away
A common habit is to write defer a.free right after a successful alloc, so cleanup runs no matter how the scope exits.
const buf = try a.alloc(u8, 64);
defer a.free(buf);create for One Value
When you need exactly one heap value, use create. It takes a type and returns a single-item pointer to fresh memory.
const node = try a.create(Node);Write Through the Pointer
create gives you a pointer, so you set the value through it with .* before using the data it points to.
node.* = Node{ .value = 42 };destroy the Pointer
Release a created value with destroy, passing the same pointer create returned. This is the partner call to create.
a.destroy(node);Match the Pairs
The rule is strict: alloc pairs with free, and create pairs with destroy. Never cross them or free a slice you did not allocate.
Free with the Same Allocator
Always free memory using the same allocator that produced it. Mixing allocators leads to corruption the compiler cannot catch.
Errors Skip the defer Body
If alloc itself fails, no memory was made, so there is nothing to free. That is why defer free comes after a successful alloc. ✅
Quick Check
Match each allocation call with its correct release call.
Recap
You learned the pairs: alloc/free for slices and create/destroy for single values, always released with the same allocator. 🎯
Frequently asked questions
Is the “alloc, free, create, destroy” lesson free?
Yes — the full text of “alloc, free, create, destroy” 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 “alloc, free, create, destroy”?
The core allocator operations. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “alloc, free, create, destroy” 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