3 ms·
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 o
by plorkyeran 3y ago
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.
- afiori 3y ago> 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:OYLghAFBqd5TKALEBjA9gEwKYFFMCWALugE4A0BIEAZgQDbYB2AhgLbYgDkAjF%2BTXRMiAZVQtGIHgBYBQogFUAztgAKAD24AGfgCsp5eiyagA%2BudTkVjVEQJDqzTAGF09AK5smUgOzknADIETNgAcp4ARtikIH4ADuhKxPZMrh5eUgDM5AlJdkJBIeFsUTEAbFbYNvlMIkQspERpnt48ftbYtil1DUSFYZHRsVb1jc0ZPNlKo33BAyVDZQCUVujupKicXADUu9sApABMmcGoHjgHmc7ThOgAdEj7mbj7WgCCr29728FE22wsYIQJYHHwAIU%2B32%2BcVIvxoECOhwAElV6OhyAdDoclk8Ie8oXsAPSE7YwuEIrEo%2BhojGInGZPFfAnbLSErS4yHMgBUEFCCgCAXpjOZpNhwnhiIA6mR6JgwGB9gBWZxMOkc/HM0jYIjrJgs9VM3b7HwAEU%2BXBW9G4iv43i4OnI6G4zm2SjWG2wmMyfHIRG0FpWAGsQIqtIZuNJ%2BGwQ2G7Q6nVx%2BEoQGG/faLeQ4LAUBgcPhiGRKNQ6IxWBxuD7BMIxBJODI5MJlGpNOnyPpDoZjGYLJVqilHEwXG4WgZAnNiqUpGHcskhONWjlErOmP0J0MeGGOl0hD0xsOJr3OjVd7MioMYhuRr15wZpr1V%2Bepys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bG22DFJQaiwGaMgdo2B8D9GIPQQGcg8lFJDBUsGRU0g7iZC0AADi0JZTIllLLSB8D4HxPgKh8XUtGDcsZPJ6SsCADs0l7F8UktpIBcSknGXRkkBw0ggA%3D https://godbolt.org/#z:OYLghAFBqd5TKALEBjA9gEwKYFFMCWALugE4A...
- plorkyeran 3y agoThe 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.
- afiori 3y agoI 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.