# Returning stack variable references from inlined functions

**URL:** <https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276>\
**Category:** Help\
**Tags:** memory-management\
**Created:** [June 21, 2026, 11:53am UTC](https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276 "2026-06-21T11:53:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![masabence](https://ziggit.dev/user_avatar/ziggit.dev/masabence/32/9187_2.png) [@masabence](https://ziggit.dev/u/masabence)\
**Post date:** [June 21, 2026, 11:53am UTC](https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276/1 "2026-06-21T11:53:45Z")

</div>

```zig
pub inline fn getStdout(io: std.Io, comptime buf_size: usize) *std.Io.Writer {
    var buf: [buf_size]u8 = undefined;
    var instance = std.Io.File.stdout().writer(io, &buf);
    return &instance.interface;
}

pub fn main(init: std.process.Init) !void {
    const stdout = getStdout(init.io, 4096);          
    try stdout.writeAll("Hello, World!\n");  
    try stdout.flush();
}

```

Is this undefined behaviour? I know returning references to local variables is illegal, but does inlining it make it safe?

---

<div class="post-metadata">

**Author:** ![Dok8tavo](https://ziggit.dev/user_avatar/ziggit.dev/dok8tavo/32/707_2.png) [@Dok8tavo](https://ziggit.dev/u/Dok8tavo)\
**Post date:** [June 21, 2026, 11:57am UTC](https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276/2 "2026-06-21T11:57:25Z")

</div>

It doesn’t. I mean it does for now but that’s not intended. Even blocks can have their stack overwritten.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [June 21, 2026, 11:58am UTC](https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276/3 "2026-06-21T11:58:31Z")

</div>

it is not supposed to be legal, but the current implementation does allow it.

You should not rely on that as it will change, local variables are intended to be confined to their scope, whether that is a function or block, inline functions are no exception.

* * *

a function to save 3 lines of code, that also does not provide any additional clarity, is not idiomatic.

Not to mention, the caller may want `instance` as it provides additional, file specific, functionality e.g seeking, or resizeing the file.

---

<div class="post-metadata">

**Author:** ![masabence](https://ziggit.dev/user_avatar/ziggit.dev/masabence/32/9187_2.png) [@masabence](https://ziggit.dev/u/masabence)\
**Post date:** [June 21, 2026, 12:00pm UTC](https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276/4 "2026-06-21T12:00:32Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [June 21, 2026, 12:03pm UTC](https://ziggit.dev/t/returning-stack-variable-references-from-inlined-functions/16276/5 "2026-06-21T12:03:31Z")

</div>

There is the caveat that in comptime it is allowed as values are reference counted.
