Back to course

FixedBufferAllocator

Full Course: Zig Programming From Zero to Hero

FixedBufferAllocator

Sometimes you want to allocate memory from a pre-allocated stack buffer rather than the heap for speed and predictability.

zig var buffer: [1024]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&buffer); const allocator = fba.allocator();

const data = try allocator.alloc(i32, 10);

This is much faster than heap allocation but limited by the size of the initial buffer.