5 ms·
That's why the author prefaced the sentence with "In effect". The effect in this case is the same as if the compiler had pretended i was initialized to a high v
by jonas21 3y ago
That's why the author prefaced the sentence with "In effect". The effect in this case is the same as if the compiler had pretended i was initialized to a high value - the loop is omitted.
- plorkyeran 3y agoIt's still fundamentally a misunderstanding of what the compiler is doing, and thinking about it that way is just going out of your way to cause confusion. The compiler saw a use of an uninitialized value, concluded that it must not be reachable as that's the only way for it to be a legal C program, and then deleted the dead code. You can make up all sorts of other behaviors that the compiler could have done which would have had the same result in a specific scenario, but why would you?
- stanleydrew 3y agoI don't think the author misunderstands what the compiler is doing, just saying that it's probably unexpected behavior from the perspective of the program's author.
- afiori 3y agoI don't think that the compiler approach is try and fix the code > it must not be reachable as that's the only way for it to be a legal C program, and then deleted the dead code. Rather it would make more sense if the approach was more like "this branch is UB so I can whatever is most convenient in terms of optimization" in this case it was merging the 2 branches but discarding the code for the UB branch. But from a behavioral point of view all these formulations describe the same result
- plorkyeran 3y agoThe 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%2BudTkVjVEQJDqzTAGF09AK5smUgOzknADIETNgAcp4ARtikIH4ADuhKxPZMrh5eUgDM5AlJdkJBIeFsUTEAbFbYNvlMIkQspERpnt48ftbYtil1DUSFYZHRsVb1jc0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mJCkF6IveemKz%2BWggXBYwFBUOFDtRSA6uJclRgmGl2AZdluWFcVdqVgwFWkFVNWtq1jXNbH7Wdd1ti9YF/VEINw06WNE1TdSs0%2BgtXbLQ6hBrXYG1bfwO17QdX1HXxDqwxd7VXVsDq3QQ918I9z2vUHH2h2V8g1n9sgA02wOtu24PdtDhhndjiPIykqOOujmObfAz6M%2Bz%2BOs8T8zc%2BTy6szTKRc%2BubPHiz%2B4LrjzP3kL9M87f/Ov0/JPc6Lr6cNikseVbAmbYCslYqzVnFaQmttY%2B2AHrA2RsTZmzIBJJY1sZKZhQIQGgNAwoA1HnWce8hJ4tibkgZMHZ6DkOwTQIgDVioGSoSodQt0WBIzxomcgpByEGG4UoGhdCGFhj4TtVh7D%2ByJktNaQB8ZuAmgIDg7Yb08rRBAYrZWqtPbQJ1nA/WpBDbG22DFJQaiwGaMgdo2B8D9GIPQQGcg8lFJDBUsGRU0g7iZC0AADi0JZTIllLLSB8D4HxPgKh8XUtGDcsZPJ6SsCADs0l7F8UktpIBcSknGXRkkBw0ggA%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.