You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Optionals have a weird behavior when unwrapping. Unwrapping with an if statement doesn't work as expected, unless you manually unwrap with the .? operator in the same block.
The example is based off of the append method.
Currently the debug prints this:
Test [1/1] test.LinkedList... Tail(TMP): 42
Tail: -63390368
Tail: 16
expected -63390368, found 42
Test [1/1] test.LinkedList... FAIL (TestExpectedEqual)
/home/gianl/zig/lib/std/testing.zig:84:17: 0x214993 in expectEqual__anon_1124 (test)
return error.TestExpectedEqual;
^
/home/gianl/projects/zig/data_structures/src/linked_list.zig:67:5: 0x214b87 in test.LinkedList (test)
try std.testing.expectEqual(list.head.?.val, 42);
^
0 passed; 0 skipped; 1 failed.
error: the following testcommand failed with exit code 1:
/home/gianl/projects/zig/data_structures/zig-cache/o/dc83372edd4bf92a75e3cb2e9aad0d79/test
If the // std.debug.print("Tmp Again: {d}\n", .{tmp.?.val}); line is uncommented, its prints this:
Test [1/1] test.LinkedList... Tail(TMP): 42
Tail: 42
Tmp Again: 42
Tail: 16
All 1 tests passed.
pubfnappend(self: *Self, val: T) void {
varnode=Node{.next=null, .prev=null, .val=val};
vartmp=self.tail;
std.debug.print("Tail(TMP): {d}\n", .{tmp.?.val});
// I should have access the the *Node inside of this blockif (tmp) |tail| {
// I would expect that tail.val should be 42std.debug.print("Tail: {d}\n", .{tail.val});
// std.debug.print("Tmp Again: {d}\n", .{tmp.?.val});tail.next=&node;
self.tail=&node;
std.debug.print("Tail: {d}\n", .{self.tail.?.val});
self.len+=1;
} else {
self.head=&node;
self.tail=&node;
self.len=1;
}
}
The text was updated successfully, but these errors were encountered:
geefuoco
added
the
bug
Observed behavior contradicts documented or intended behavior
label
Aug 5, 2023
You're storing pointers to locals (node is allocated on the stack of prepend/append), which go out-of-scope after their function returns. This is illegal behavior that is currently unchecked (see #3180).
Once a function returns, any Pointers to variables in the function's stack frame become invalid references, and dereferencing them becomes unchecked Undefined Behavior.
You would need to heap allocate the Nodes if you want to them to live past the function. See also std.SinglyLinkedList for an idea of how a linked list can be implemented in a way that's agnostic about how Nodes are allocated.
Side note: this type of thing could be run past one of the community spaces first to double check that it's a bug.
jacobly0
added
question
No questions on the issue tracker, please.
and removed
bug
Observed behavior contradicts documented or intended behavior
labels
Aug 5, 2023
Zig Version
0.11.0-dev.4217+e8fa19960
Steps to Reproduce and Observed Behavior
Optionals have a weird behavior when unwrapping. Unwrapping with an
if
statement doesn't work as expected, unless you manually unwrap with the.?
operator in the same block.The example is based off of the
append
method.Currently the debug prints this:
If the
// std.debug.print("Tmp Again: {d}\n", .{tmp.?.val});
line is uncommented, its prints this:Expected Behavior
That the unwrapping of the optional would work
The text was updated successfully, but these errors were encountered: