liveBook · Manning

Buffer

As explained in Systems Programming with Zig

A buffer is a temporary memory area that holds data before it is processed or transferred.

A buffer is temporary storage for data. In Zig, functions often accept a caller-provided buffer as a mutable byte slice, []u8. For example, a stdout writer can collect output in a buffer, reducing the number of blocking writes. section 2.2.4

An array declared with undefined can supply buffer space without initializing its contents. Write data before reading it: reading an uninitialized value produces unpredictable results and can create security problems. section 2.2.4

Listing 2.12 Hello world, with buffers explained
const std = @import("std");

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    const stdout_file = std.Io.File.stdout();
    var stdout_buffer: [1024]u8 = undefined; #1
    var stdout_writer = stdout_file.writer(io, &stdout_buffer); #2
    const stdout = &stdout_writer.interface;

    try stdout.print("All your codebase still belong to us!\n", .{}); #3
    try stdout.flush(); #4
}

This stdout writer receives a 1,024-byte array, prints into the buffered writer, and flushes the output. section 2.2.4

Buffers can also provide short-lived memory across a C interface. Pass a pointer and length, turn them into a Zig slice, and initialize std.heap.FixedBufferAllocator with that slice. Its allocator interface lets code build an ingredient array in caller-provided memory; an append that exceeds the buffer returns would_overflow. section 5.4.2

Listing 5.19 Retrieving recipes with Benito
export fn recipe_get(
    book: *const RecipeBook,
    recipe_id: RecipeId,
    buf_ptr: [*]u8, #1
    buf_len: usize, #1
    result: *RetrievedRecipe,
) Success {
    const buffer = buf_ptr[0..buf_len]; #2
    var fba: std.heap.FixedBufferAllocator = .init(buffer); #3
    const buf_ally = fba.allocator(); #3

    const recipe = book.getRecipe(recipe_id) catch |e| {#4
        return translateErr(e);
    };

    var ingredients_copy: std.ArrayList(RetrievedIngredient) = .empty; #5
    var ingredients = book.iterateIngredients(recipe_id);
    while (ingredients.next()) |ing| {
        ingredients_copy.append(buf_ally, .{ #6
            .quantity = ing.quantity.ptr,
            .name = ing.name.ptr,
        }) catch return .would_overflow; #7
    }

    result.* = .{ #8
        .name = recipe.name.ptr,
        .author = recipe.author.ptr,
        .instructions = recipe.instructions.ptr,
        .ingredients = ingredients_copy.items.ptr,
        .ingredient_count = ingredients_copy.items.len,
    };
    return .ok; #9
}

The recipe retrieval function converts a pointer and length into a buffer, then uses a fixed buffer allocator to build the ingredient array. section 5.4.2

Buffer size is a practical trade-off. Reading an entire file into an allocated buffer before writing it to stdout requires an allocation large enough for the file. Avoiding that allocation removes a potential out-of-memory failure at that stage. section 3.4.4

How Systems Programming with Zig covers it

How deep it goes
Explains buffer declaration, initialization hazards, output flushing, and fixed-buffer allocation, without detailing allocator internals.
Examples
Uses a buffered stdout greeting, whole-file reading, and recipe retrieval into caller-provided memory.
What it assumes
Assumes familiarity with Zig arrays and slices while introducing buffers and the undefined value.
What you can do after
Apply buffers using Zig's stdout writer and std.heap.FixedBufferAllocator, handling uninitialized memory and buffer overflow.
How it connects
Connects buffers to arrays, slices, allocation, buffered I/O, and foreign function interfaces.

Also explained in

Compare how 2 books explain buffer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.