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
The following test says it all. I did my best to search for previous bug reports on this, but I just couldn't find anything similar, so apologies if I missed something obvious somewhere.
If I try to produce an array of u8's, with values 1, 2, and 3, the following code works. But if I produce an array of pointers to u8's, then all of it's values are initialized to 3, instead of 1, 2, and 3.
error: 'main.test.array initialization' failed: expected 1, found 3
/home/user/.zvm/master/lib/std/testing.zig:93:17: 0x103bec8 in expectEqualInner__anon_2083 (test)
return error.TestExpectedEqual;
^
/tmp/zig-test/src/main.zig:77:5: 0x103c22e in test.array initialization (test)
try std.testing.expectEqual(1, src[0].*);
^
error: while executing test 'main.test.array initialization', the following test command failed:
This was tested using zig build test --summary all in a fresh zig init project.
Expected Behavior
Array initialization should work reliably for pointers, just like it does for non-pointers.
The text was updated successfully, but these errors were encountered:
adworacz
added
the
bug
Observed behavior contradicts documented or intended behavior
label
Mar 2, 2024
adworacz
changed the title
Initialization of array of pointers results in different values than u8
Initialization of array of pointers to u8 results in different values than array of u8
Mar 2, 2024
The pointers you store into src point to a local variable (bar) which goes out of scope on each loop iteration. This invalidates the value, since the lifetime of runtime-known local variables is tied to their scope. (In practice, what's happening here is that the same stack memory is reused for bar each iteration.)
I would give specific advice if I could, but I don't know enough about your use case to do so. If you'd like help here, I'd encourage you to join a Zig community.
I think this behavior is still very surprising, particularly given Zig's argument for "no hidden behavior". Looking at the code above, I wouldn't have been able to tell that the same stack memory is going to be reused for the entire array.
Considering that the scope is lost, I'd almost expect for the compiler to complain somehow.
Regardless, I'll reach out to the Zig communities you posted as well.
However, I suspect I'm not the only developer to be stung by this surprising behavior.
Zig Version
0.12.0-dev.3123+147beec7d
Steps to Reproduce and Observed Behavior
The following test says it all. I did my best to search for previous bug reports on this, but I just couldn't find anything similar, so apologies if I missed something obvious somewhere.
If I try to produce an array of u8's, with values 1, 2, and 3, the following code works. But if I produce an array of pointers to u8's, then all of it's values are initialized to 3, instead of 1, 2, and 3.
Here's the code:
Resulting error:
This was tested using
zig build test --summary all
in a freshzig init
project.Expected Behavior
Array initialization should work reliably for pointers, just like it does for non-pointers.
The text was updated successfully, but these errors were encountered: