#List Decay when returned

1 messages · Page 1 of 1 (latest)

cerulean cape
#

Hey this is probably showing my gross ignorance but I have code that seems to decay an array when returned by a function. My code looks kind of like this :

    var res = [_]usize{0} ** 8;
    //Initializing
    std.debug.print("{any}\n", .{});
    return res[0..ActualLen];
}

fn Otherfunc() void {
    const res = get_surrounding_entries();
    std.debug.print("{any}\n", .{res});
}

The output then reads :

{140733924216688, 19378517, 140733924220336, ...} //Both arrs do have 8 items, albeit one is very wrong.

The 'get_surrounding' function does use a memory allocated array inside it. I am on the latest dev version. The get_surr func is a member of a struct.

I would paste my source, but it is on a computer that isn't really connected to Google or the like. I did copy the exact numbers down if it helps. (It is also based off of a random call somewhere down the line so the numbers do change, but maybe the computer forgets it's a usize and reads it as a large or something, IDK).

spare elm
#

get_surrounding_entries is returning a slice that references a local variable (res), so that is invalid as soon as the function returns

cerulean cape