1

Are there real world cases where compilers in C produced object code that functioned almost identically as the source code described, but in the end turned out that the optimizations turned (or could turn) disastrous?

Anna K.
  • 95
  • 10
  • Unless there is a bug in the compiler itself, what you get out is what you put in. – Matt Clark May 26 '17 at 16:34
  • That would be caused by either a bug in the compiler, or lack of knowledge or insight of the programmer who thought he didn't have to hint the compiler. – Cheatah May 26 '17 at 16:36
  • Heavy optimization can still produce wrong code, because of bugs in the compilers. Heavy optimization can also produce code that is *slower* or otherwise less efficient than lower optimization levels. Very small mistakes in the source could also multiply into large problems due to optimizations, especially when undefined behavior is involved. – Some programmer dude May 26 '17 at 16:36

1 Answers1

0

Example: if(something || i++). Let's assume something is true. An optimization would be to just skip i++ since the or statement is already true. It depends on the compiler and its configuration whether the second statement is actually executed or not. So this would be the one example I can think of where the compiler optimizations could lead to 'unexpected' results.

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • Actually, that's what I would expect it to do / stop testing @ success – KevinDTimm May 26 '17 at 17:21
  • 1
    You are correct, but using statements that changes the state of a variable on a conditional predicate is bad practice. – luizfzs May 26 '17 at 18:52
  • The short-circuit is guaranteed. If the compiler produces the code which evaluates `i++` when `something` is `true`, it is a broken compiler. – user58697 May 26 '17 at 19:34
  • You need to specify if you are talking C/C++, because if you do, then your example is simply wrong. The short-circuit is guaranteed by the standard therefore if you are using a compiler which unconditionally increments `i` you are not using the language you think you are using. – Vroomfondel Nov 11 '17 at 21:41