Re:When you see a Heisenbug in C, change your compiler's opt...
by cpncrunch on Saturday March 01, 2014 @06:21PM
(#7326647)
Agreed. Many times I've seen a bug disappear when I've added printfs or turned on -g, but I've never, ever seen an optimization bug in a C/C++ compiler.
The only time I've seen optimization bugs is in the emscripten compiler, but I'm sure those will be ironed out eventually (and the current version seems pretty stable).
Re:When you see a Heisenbug in C, change your compiler's opt...
by userbinator on Saturday March 01, 2014 @09:14PM
(#7327361)
In nearly 2 decades I have seen only one compiler bug, in MSVC6, which was related to large switch statements.
The most successful way of finding what's causing these "heisenbugs" is to NOT change the binary, but just load it in the debugger as-is, make a guess at where it's failing, and set a few breakpoints/watchpoints. Either the compiler is not generating the code that you expect, or your expectations of the language were wrong, and both cases are fairly clear from looking at the generated code.
Re:When you see a Heisenbug in C, change your compiler's opt...
by infogulch on Saturday March 01, 2014 @08:40PM
(#7327244)
That has happened to me as well. There's a bug until I add a printf or enable debugging, or (paradoxically) go from -O0 to -O2. What's going on here? Is there some specific class of UB that I'm invoking to get this infuriating bug?
Re:When you see a Heisenbug in C, change your compiler's opt...
by cpncrunch on Sunday March 02, 2014 @12:30AM
(#7327848)
I think it's usually just accessing out of bounds memory / uninitialized read, and -g/-O just happens to change the memory layout enough to cause/prevent the crash.
Re:When you see a Heisenbug in C, change your compiler's opt...
by username223 on Saturday March 01, 2014 @09:23PM
(#7327382)
At some point many hundreds of years ago, the law became too complicated for people to navigate on their own, so lawyers (and Law French) were created. How long will it be before there are paid language lawyers, arguing for compilers vs. programs in an ANSI/ISO "court"? There's probably money to be made in "optimizer trolling," come to think of it.
That is 99.9% rubbish. By which I mean in 99.9% of cases, when code works at -O0 and not at higher optimization levels your code has a bug, almost certainly involving invoking undefined behaviour.
Certainly it is possible to find optimiser bugs, I have done it myself, but it is many times more common for your code to have bugs. Now, you could argue that optimizers shouldn't make such extensive use of undefined behaviour, which leads to these kinds of bugs, but it is perfectly valid for a C compiler to do so.