alloc, free, create, destroy
Operasi inti pengalokasi.
alloc, free, create, destroy 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.
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. 🎯
Pertanyaan yang Sering Diajukan
Apakah pelajaran “alloc, free, create, destroy” gratis?
Ya — teks lengkap “alloc, free, create, destroy” 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 “alloc, free, create, destroy”?
Operasi inti pengalokasi. 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 “alloc, free, create, destroy” 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
- Mengapa Zig Tidak Memiliki Alokasi Tersembunyi
- Antarmuka Pengalokasi
- alloc, free, create, destroy
- Daftar Dinamis dengan ArrayList