Unitialized memory using slice of stack allocated array

Hello everyone,

I am trying to code an iterator for a container holding a fixed size array. However, when the iterator tries to access the array values using a slice, it finds unitialized values.

Here is a minimal example of what is happening.


test "iterator" {
    var container = Container.init();

    var it = container.iterator();
    while (it.next()) |value|
        _ = value;


const Container = struct {
    items: [3]usize,

    pub fn init() Container {
        return .{ .items = [3]usize{ 2, 15, 59 } };

    pub fn iterator(self: Container) Iterator {
        std.debug.print("struct: {any}\n", .{self.items});
        return .{ .slice = &self.items };


const Iterator = struct {
    slice: []const usize,

    pub fn next(it: *Iterator) ?usize {
        std.debug.print("iterator: {any}\n", .{it.slice});
        return null;


struct: { 2, 15, 59 }
iterator: { 140731037544200, 140731037544168, 140731037544096 }

How can the iterator access the correct values?

fn iterator(self: Container)

You are passing the container by value, so it’s probably making a local copy of the Container.

return .{ .slice = &self.items };

Then you store a pointer to that local copy in the iterator.
Right after the return, the local copy gets invalid, so whenever you call some function on the iterator after that it tries to reference invalid memory.

To avoid you should pass by reference:

fn iterator(self: *Container)

Thank you very much, it worked!