0Pricing
Zig Academy · Pelajaran

FixedBufferAllocator tanpa Heap

Alokasikan dari buffer stack.

FixedBufferAllocator tanpa Heap adalah pelajaran Zig Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Zig Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Zig Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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. 🎯

Pertanyaan yang Sering Diajukan

Apakah pelajaran “FixedBufferAllocator tanpa Heap” gratis?

Ya — teks lengkap “FixedBufferAllocator tanpa Heap” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Zig Academy, upgrade ke CoddyKit PRO. Kursus Zig Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “FixedBufferAllocator tanpa Heap”?

Alokasikan dari buffer stack. Kamu berlatih Zig Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Zig Academy?

Tidak diperlukan pengalaman sebelumnya. Zig Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.

Berapa lama pelajaran “FixedBufferAllocator tanpa Heap” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Zig Academy ini?

Ya. Setiap pelajaran Zig Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. GeneralPurposeAllocator untuk Keamanan Debug
  2. Arena Allocator untuk Pembebasan Massal
  3. FixedBufferAllocator tanpa Heap
  4. Memilih Allocator untuk Setiap Beban Kerja
← Kembali ke Zig Academy