Comment by nilamo

7 hours ago

This looks like a long back and fourth, that can easily be solved by a minute or two on godbolt...

> that can easily be solved by a minute or two on godbolt...

Unfortunately it's not that simple when it comes to UB. If the snippet in question does in fact exhibit UB then there's no guarantee whatever Godbolt shows will generalize to other programs/versions/compilers/environments/etc.

  • That's very funny to me.

    A) x is always removed.

    B) no, it's never removed if volatile.

    But neither person can prove what a compiler will actually do, despite claiming they'll always act a certain way given 5 lines of code.

    • No, claim A is 'x may be removed by a conforming C compiler'. Whether any given version of a given compiler actually does so in any given circumstance is a different question (the answer being: probably not, because while this is undefined behaviour it's not likely something that is going to be flagged as such by a compiler's optimizer. Also, from some testing with GCC and forcing a null point dereference, it seems like volatile at least does win in that case with the current version of it x86, and it dutifully emits the null pointer dereference and then the 'ud2' instruction instead of the rest of that execution path).

    • I have watched a compiler flip between emitting the code I expected (despite it having UB), and emitting unexpected code after a minor update.

      What you observe a compiler do when there's UB is not at all something you can rely on.

    • Also, at behavioural edges what you'll see on Godbolt is compiler bugs. So you learn nothing about what should happen.

      All popular modern C++ compilers have known bugs and while I'm sure there are C compilers with no known bugs that will be because nobody tested very hard.

    • I made the weaker claim that x can be removed. This is something I could prove with compiler output but I would have to find a compiler willing to make this optimization which is not something I can guarantee.