0Pricing
Zig Academy · Lektion

alloc, free, create, destroy

Die grundlegenden Allocator-Operationen.

alloc, free, create, destroy 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.

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

Häufig gestellte Fragen

Ist die Lektion „alloc, free, create, destroy“ kostenlos?

Ja — der vollständige Text von „alloc, free, create, destroy“ 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 „alloc, free, create, destroy“?

Die grundlegenden Allocator-Operationen. 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 „alloc, free, create, destroy“?

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

  1. Warum Zig keine verborgenen Allokationen hat
  2. Die Allocator-Schnittstelle
  3. alloc, free, create, destroy
  4. Dynamische Listen mit ArrayList
← Zurück zu Zig Academy