• I would hesitate to say that U(ndefined)B exists for the purpose of compiler optimization. You can have a Turing-decidable language and still optimize within it, and that requires the decider to be total, so the semantics of every behavior have to be well-defined by definition. All you need is a notion of what behavior is and is not observable. My personal crackpot theory as to why the term “undefined behavior” has stuck around is aversion to the idea of “nonstandard behavior.” Crashing when pointers to different types alias is often way better than introducing memory corruption via ordering, or worse, coherence mismatches. If this was deemed “checked illegal behavior,” and this program blew up the planet:

    volatile int q;
    
    void foo(int *a, float *b) {
        *a = 5;
        *b = 6;
    }
    
    int main() {
        foo(&q, (float*)&q);
        blow_up_the_planet();
    }
    

    That may end up being a nightmare for someone. However, if the behavior is undefined, then it is perfectly legal for the compiler to decide to blow up the planet.


    score 1 · canonical

    • (to clarify, my claim is “can be used for”, not “exists for”, I am agnostic about UB’s original intended purpose)

      All you need is a notion of what behavior is and is not observable.

      That’s the issue for C though, everything is observable, and why I think any discussion about optimization in the context of C UB is ultimately a red herring. Consider:

      int a = 1;
      int b = 2;
      int* a_ptr = &a;
      int* feeling_lucky_ptr = a_ptr + 1;
      *feeling_lucky_ptr += 1;
      printf("%d", b);
      

      You just can’t explain what a non optimizing compiler would do to this program, without some notion of (Unchecked) Illegal Behavior.


      score 1 · canonical

      • Are you proposing the standard itself imposes what is and is not checked? Or just what is illegal? If the former, you still run into the problem I proposed if you ever deem any behavior to be “checked illegal.” In the latter case, why is “maybe checked illegal behavior” practically any better than “undefined,” if the answer as to what happens is still “I dunno?”

        That’s the issue for C though, everything is observable

        And as far as I understand it, please correct me if I am mistaken, this is not true. Just because you gave a a memory address does not mean that its value is necessarily observable.

        If what you mean by “unoptimizing compiler” is a naive compiler that translates the code to the letter like a macro assembler, then you could very easily explain what that code does, just not through the lens of the abstract machine. It writes happens to be at a_ptr + 1, which is a perfectly reasonable statement given that the abstract machine is not in question here.

        To loop back, is the behavior I just described “non-standard” (not explained by the standard), “undefined” (also not explained by the standard), or “unchecked and illegal” (I don’t know exactly what this means)? Practically, what is the difference?


        score 1 · canonical