FixedBufferAllocator ohne Heap
Allokieren Sie aus einem Stack-Puffer.
FixedBufferAllocator ohne Heap ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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.
Allocate Without the Heap
A FixedBufferAllocator hands out memory from a buffer you already own. No heap, no operating system call, just your own bytes. 🧱
You Supply the Buffer
You start with a plain array, often on the stack. That fixed region is the entire pool the allocator is allowed to give out.
var buffer: [1024]u8 = undefined;Wrap the Buffer
Pass your buffer to init to create the allocator. From now on, every allocation comes out of those exact bytes.
var fba = std.heap.FixedBufferAllocator.init(&buffer);Get Its Allocator
Call allocator() as usual to get the interface. Code using it cannot tell it is backed by a fixed buffer rather than the heap.
const a = fba.allocator();It Bumps a Pointer
Allocation just moves an offset forward through the buffer. That makes it extremely fast and completely predictable in cost.
It Can Run Out
The buffer has a fixed size, so a request can fail with OutOfMemory when there is no room left. You handle that like any allocation error.
Reset to Reuse
Call reset() to move the offset back to the start. The same buffer is now empty again, ready for a fresh round of allocations.
fba.reset();Great for Embedded
With no heap involved, this allocator suits embedded systems, kernels, and any place where dynamic memory is unavailable or forbidden.
Mind the Lifetime
If the buffer lives on the stack, its memory vanishes when the function returns. Never let allocations from it outlive that buffer.
Pair It With an Arena
A common trick is to wrap a fixed buffer in an arena, giving you bulk free semantics over a region that never touches the heap at all.
A Typical Setup
The recipe is short: declare a buffer, init the allocator over it, then allocate until full and optionally reset to start over.
var buf: [256]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
const a = fba.allocator();Quick Check
Think about where a FixedBufferAllocator gets its memory.
Recap
You met the FixedBufferAllocator: it serves memory from a buffer you own, never touches the heap, and resets in one cheap step. 🎯
Häufig gestellte Fragen
Ist die Lektion „FixedBufferAllocator ohne Heap“ kostenlos?
Ja — der vollständige Text von „FixedBufferAllocator ohne Heap“ 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 „FixedBufferAllocator ohne Heap“?
Allokieren Sie aus einem Stack-Puffer. 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 3 von 4.
Wie lange dauert die Lektion „FixedBufferAllocator ohne Heap“?
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
- GeneralPurposeAllocator für Debug-Sicherheit
- Arena-Allocator für gebündelte Freigaben
- FixedBufferAllocator ohne Heap
- Den passenden Allocator pro Workload wählen