liveBook · Manning

Buffering

As explained in Systems Programming with Zig

Buffering is the temporary storage of input or output data before it is processed or transferred.

Buffering stores output bytes temporarily instead of sending each write to a file immediately. The runtime typically writes them all at once when a newline or byte-count threshold is reached; writing stored data is called flushing. Automatic buffering is implicit, while Zig lets programmers choose whether it happens and where the buffer memory comes from. section 1.6.2

Listing 1.2 All your codebase still belong to us
const std = @import("std");

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

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

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

The Zig example supplies a 1,024-byte buffer to a stdout writer, writes a newline-terminated message, and explicitly flushes it. section 1.6.2

How Systems Programming with Zig covers it

How deep it goes
Defines buffering and flushing, explains newline or byte-count triggers, and contrasts implicit runtime behavior with explicit Zig control.
Examples
Uses a Zig stdout writer with a 1,024-byte buffer, a newline-terminated message, and an explicit flush.
What you can do after
Operate/configure buffering by supplying a stdout buffer and flushing the writer explicitly.
How it connects
Connects buffering with stdout, stderr, file I/O, flushing, and std.Io.

Also explained in

Compare how 8 books explain buffering →

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.