# Stack allocated strings

**URL:** <https://ziggit.dev/t/stack-allocated-strings/17289>\
**Category:** Brainstorming\
**Tags:** memory-management\
**Created:** [August 19, 2026, 10:28am UTC](https://ziggit.dev/t/stack-allocated-strings/17289 "2026-08-19T10:28:13Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![pachde](https://ziggit.dev/user_avatar/ziggit.dev/pachde/32/2485_2.png) [@pachde](https://ziggit.dev/u/pachde)\
**Post date:** [August 19, 2026, 6:26pm UTC](https://ziggit.dev/t/stack-allocated-strings/17289/22 "2026-08-19T18:26:04Z")

</div>

> [@matklad](#):
>
> I am not a huge fan of manually computed bounds, as they will invariably become incorrect when the surrounding code changes

Understandable. It’s another item in a long list of possible invariants one must maintain as you modify code (“things to think about”). Invariants are what assertions are for. The important part is not necessarily remembering to update your buffer size but adding the checks to detect when your buffer has become too small (incorrect) or too big (wasteful).

---

<div class="post-metadata">

**Author:** ![VoilaNeighbor](https://ziggit.dev/user_avatar/ziggit.dev/voilaneighbor/32/745_2.png) [@VoilaNeighbor](https://ziggit.dev/u/VoilaNeighbor)\
**Post date:** [August 21, 2026, 7:07am UTC](https://ziggit.dev/t/stack-allocated-strings/17289/23 "2026-08-21T07:07:34Z")

</div>

I think there is one small trick with allocators that could make life much better.

Make `FixedBufferAllocator.reset` receive an optional checkpoint index, so that the caller can do this:

```zig
const checkpoint = fba.checkpoint(); // just gets the end index
defer fba.resetTo(checkpoint);

...

```

This avoids having to fiddle with ad hoc on stack buffers, while keeping almost zero overhead.

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [August 22, 2026, 2:00am UTC](https://ziggit.dev/t/stack-allocated-strings/17289/24 "2026-08-22T02:00:42Z")

</div>

> [@matklad](#):
>
> - I don’t want to waste more stack space than I really need.

One more solution to this problem – stack-allocated values with a small expected size but a large upper bound, harming memory locality – is an `@alloca(size, upper_bound)`. It could trivially be implemented as allocating a fixed `upper_bound`, but in release mode it could allocate less.

---

<div class="post-metadata">

**Author:** ![invlpg](https://ziggit.dev/letter_avatar_proxy/v4/letter/i/13edae/32.png) [@invlpg](https://ziggit.dev/u/invlpg)\
**Post date:** [September 10, 2026, 10:09am UTC](https://ziggit.dev/t/stack-allocated-strings/17289/25 "2026-09-10T10:09:33Z")

</div>

> [@cancername](#):
>
> but in release mode it could allocate less.

But why? If it’s statically known that there’s a safe upper bound, then why not always allocate that amount?

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 12, 2026, 1:47pm UTC](https://ziggit.dev/t/stack-allocated-strings/17289/26 "2026-09-12T13:47:50Z")

</div>

As I said, it hurts memory locality (related memory is further apart, leading to far cache misses) and for dynamic stacks will grow the stack much faster.

---

<div class="post-metadata">

**Author:** ![matklad](https://ziggit.dev/user_avatar/ziggit.dev/matklad/32/2462_2.png) [@matklad](https://ziggit.dev/u/matklad)\
**Post date:** [October 9, 2026, 1:15pm UTC](https://ziggit.dev/t/stack-allocated-strings/17289/27 "2026-10-09T13:15:43Z")

</div>

A variation using the same buffer for several prints, using [Marler’s Maneuver](https://ziggit.dev/t/how-to-enforce-function-call-ordering/13359) of passing a `comptime` mutable _pointer_ to size bound:

```zig
fn f(x: u32, y: u64) void {
    comptime var buffer_size: u16 = 64;
    var buffer = stack_buffer(buffer_size);

    const a = buffer.print(&buffer_size, "Hello, {d}", .{x});
    const b = buffer.print(&buffer_size, "Hi, {d}", .{y});
    std.debug.print("{s}\n{s}", .{a, b});

    // main.zig:31:25: error: overflow of integer type 'u16' with value '-20'
    // const c = buffer.print(&buffer_size, "noooooooooo {d} {d}", .{x, y});
    // std.debug.print("{s}\n", .{c});
}

```

```zig
fn stack_buffer(comptime size_max: u16) StackBufferType(size_max) {
    return .{};
}

fn StackBufferType(comptime size_max: u16) type {
    return struct {
        buffer: [size_max]u8 = undefined,
        size: u16 = 0,

        pub inline fn print(self: *@This(), comptime size_left: *u16, comptime fmt: []const u8, args: anytype, ) []const u8 {
            size_left.* -= comptime print_size_max(fmt, @TypeOf(args));
            return self.print_runtime(fmt, args);
        }

        fn print_runtime(self: *@This(), comptime fmt: []const u8, args: anytype) []const u8 {
            const result = std.fmt.bufPrint(self.buffer[self.size..], fmt, args) catch |err| switch (err) {
                error.NoSpaceLeft => unreachable,
            };
            self.size += @intCast(result.len);
            return result;
        }

        fn print_size_max(comptime fmt: []const u8, Args: type) u16 {
            // The same as in the OP. 
        }
    };
}

```

---

<div class="post-metadata">

**Author:** ![psznm](https://ziggit.dev/user_avatar/ziggit.dev/psznm/32/8377_2.png) [@psznm](https://ziggit.dev/u/psznm)\
**Post date:** [October 9, 2026, 1:23pm UTC](https://ziggit.dev/t/stack-allocated-strings/17289/28 "2026-10-09T13:23:20Z")

</div>

That is very interesting. I [was under the impression](https://ziggit.dev/t/static-comptime-variable-preserve-state-between-two-comptime-function-calls/17691) that no information can be preserved between two calls of the same comptime function. I guess `inline` was the piece I was missing.

[Previous page](https://ziggit.dev/t/stack-allocated-strings/17289.md?page=1)
