Die Allocator-Schnittstelle
Ein Vertrag für alle Speicherquellen.
Die Allocator-Schnittstelle ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Die Allocator-Schnittstelle“ kostenlos?
Ja — der vollständige Text von „Die Allocator-Schnittstelle“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Die Allocator-Schnittstelle“?
Ein Vertrag für alle Speicherquellen. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Zig Academy zu starten?
Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Die Allocator-Schnittstelle“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?
Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Warum Zig keine verborgenen Allokationen hat
- Die Allocator-Schnittstelle
- alloc, free, create, destroy
- Dynamische Listen mit ArrayList