Hacker News new | past | comments | ask | show | jobs | submit login

The compiler isn't trying to "fix" anything and I'm not sure where that idea came from. The core concept is that the compiler assumes the program is valid and optimizes based on that assumption. If UB would occur if certain values are passed as an argument to a function, the compiler assumes that those values won't be passed. If UB occurs unconditionally in a function, the compiler assumes that the function won't be called.



> The core concept is that the compiler assumes the program is valid

I was referring to this.

IIRC the spec language is that the compiler is free to assume that UB never happens, but from an operational perspective I believe that the compiler simply stop caring about those cases.

by this I mean that for code like

    #include <stdio.h>

    int main() {
        printf("Hello, ");
        0/0;
        *(NULL);
        printf("World!\n");
        return 0;
    }
most compiler will just pretend that bad lines did not exists

https://godbolt.org/#z:OYLghAFBqd5TKALEBjA9gEwKYFFMCWALugE4A...


The compiler is not trying to fix your program. It is just assuming that somehow those lines are never hit, and then is deleting the dead code to make your binary smaller.


I agree, I was only disagreeing with this view:

> > The core concept is that the compiler assumes the program is valid

I believe that it is more correct to say that the compiler is unrestricted in what it does with UB, so it handles it in the most convenient way (often toward optimizing binary size, runtime performance, and/or compilation time)

By reductio ad absurdum assuming that the program is valid would give the same behaviour. I just believe that it is not what happens operatively.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: