3 ms·
To me the stupid thing is the abuse of undefined behavior for changing the semantic of the code. The fact that a behavior is not defined in the standard doesn't
by alerighi 5y ago
To me the stupid thing is the abuse of undefined behavior for changing the semantic of the code. The fact that a behavior is not defined in the standard doesn't mean that on a particular hardware platform it doesn't have a particular meaning (and most C programs doesn't need to be portable, since C it's mainly used for embedded these days and thus you are targeting a particular microcontroller/SOC).
These optimizations leave for C++ folks. C doesn't need all of that, just leave it as the "high level assembler" that it was in the old days, where if I write an instruction I can picture the assembler output in my mind.
Optimizers should not change the code semantics to me. Unfortunately with gcc it's impossible to rely on optimizations, so the only safe option is to turn them off entirely (-O0).
- pcwalton 5y ago> These optimizations leave for C++ folks. C doesn't need all of that Yes, it does. In fact C needs it more, because of the "for (int i = 0; i < n; i++)" idiom. At least idiomatic C++ code uses iterators.
- astrange 5y agoApproximately the entire software stack on any PC is shipped with gcc/clang/MSVC with optimizations turned on, and yet the Earth keeps turning.
- gHosts 5y agoSo define the behaviour and get it pushed through the standards committee. Or even define the behavior and get your compiler writer to implement it. ps: If I index past the end of the array... what behaviour are you going to define?