How to explain the behavior of modifying the value referenced by the result of @constCast?

In debug mode, Zig faithfully carried out this value modification.

In addition, from the compilation results, it can be seen that before calling the print function, there is no modification to the rdi register, which means that this print function does not take any input parameters; that is, the printed 1 1 1 has already been hard-coded into the print function at compile time. This is the reason why the printed result is the old value.

As for the reason it didn’t crash, we can check from the compiled output which section n was linked into.

npc1054657282@localhost:~/projects/zig-tests$ nm test | grep "test\.n"
000000000119acc0 d test.n

It is linked into the .data section instead of .rodata. This is the reason there was no crash. A crash in Windows may mean that Windows is stricter during linking.

5 Likes