Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's not just the function that becomes undefined, it's the entire program. There's literally nothing that the compiler is supposed to do following undefined behavior.


Is the undefined behavior applicable at runtime or at compile time ?

i.e. Is it undefined behavior if there are code paths that might access an array out of bounds, but at runtime it actually never would ?


That's the exact thing that the compiler is doing - if there are code paths that might access an array out of bounds, then the compiler is assuming that in runtime it actually never would happen.

With this assumption you might even deduce that

    int f(int x) {
      if (x != 42) undefined_behavior;
      return x;
    }
is the equivalent of

    int f(int x) {
      return 42;
    }
since (according to the assumption) in runtime it would always be called in a way that doesn't reach the undefined behavior, i.e., with x=42. And, of course, the many similar assumptions about array boundaries, pointer nullabilities, numbers not reaching overflow, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: