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
This output is normal. It's undefined behavior to copy a frame from one place to another. Zig uses Result Location Semantics (#287, #2765, #2761), so frames[i] = async task(); actually constructs the frame in frames[i] and no copy is performed. Frames are planned to be marked pinned (#7769), which will make it a compile error to copy them.
The reason this breaks in this case is also due to result location semantics. The frame contains a pointer to the location where it should store its result. This pointer is initialized at the async keyword, to point to a space reserved within the frame. (it can be made to point elsewhere with the @asyncCall builtin, or by calling an async function without the async keyword.) In amain1, it is initialized to point to stack memory in the frame variable. Since all three functions are initialized over the same memory, all of their result locations point to this variable, and the third one writes the value 2 last. Then the results are retrieved through the result location pointer at the await. These pointers were copied by value, so they still point to the stack memory for frame. This memory is no longer valid, but it hasn't been reused, so it still contains the value 2. All three frames read this value.
I've started today with zig, sorry if this is the wrong place to ask
Np, welcome! The preferred place for questions like this is a community location like the IRC or Discord.
I've started today with zig, sorry if this is the wrong place to ask.
Maybe there is something I don't understand.
Given this code (which I know is not idiomatic):
I expected functions
amain1
andamain2
to behave the same, the only difference between them being:amain1
amain2
But as you can see on output:
the first version seems to store the last frame for all indexes in the
frames
array. The second version behaves the way I expected.If this is a correct behavior, I don't mind getting an explanation :)
The text was updated successfully, but these errors were encountered: