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
Below is a link to the compiler explorer page containing the same code in both zig and c++ alongside the outputted assembly and printed result. https://godbolt.org/z/bTYGqWqE9
You can see that c++ outputs 1 whereas zig outputs 2. Taking a look at the assembly, gcc achieves this result by saving the previous value of the example union to eax before overwriting the memory in that address. Zig, however, overwrites the address of the previous value before saving it anywhere, giving the unexpected result of 2.
Expected Behavior
The expected behavior is exactly what gcc does in the linked compiler explorer page. However, if zig overwriting the previous result without saving it is intentional, I would have expected there to be a compiler error. Instead, zig failed silently.
The text was updated successfully, but these errors were encountered:
HulluKana
added
the
bug
Observed behavior contradicts documented or intended behavior
label
Feb 18, 2025
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
main.zig
:run
zig build run
and get 2 as outputBelow is a link to the compiler explorer page containing the same code in both zig and c++ alongside the outputted assembly and printed result.
https://godbolt.org/z/bTYGqWqE9
You can see that c++ outputs 1 whereas zig outputs 2. Taking a look at the assembly, gcc achieves this result by saving the previous value of the example union to eax before overwriting the memory in that address. Zig, however, overwrites the address of the previous value before saving it anywhere, giving the unexpected result of 2.
Expected Behavior
The expected behavior is exactly what gcc does in the linked compiler explorer page. However, if zig overwriting the previous result without saving it is intentional, I would have expected there to be a compiler error. Instead, zig failed silently.
The text was updated successfully, but these errors were encountered: