Ok, folks. I’ve never encountered this before and it boggles the mind and is illogical. I have a somewhat complex loop and I want to try and see if everything is working by putting some printf statements. I look the intermediate products using printf and verify that the answer is ok. Then, when I comment out the printf to the intermediate products, the answer is WRONG. Has anyone ever encountered this? This is driving me insane and I don’t see how the printfs could change an answer…. X_x If it helps, I am using a c/c++ compiler for a DSP. Thanks for any advice..
Here is a snippet…
printf("splitBackground = %d, numWindowPoints = %d\n", splitBackground, numWindowPoints);
splitBackground = splitBackground/numWindowPoints;
printf("%d ", splitBackground);
This is good but when I comment out the first line of code, it turns out to be hugely incorrect. 🙁
Most likely you’ve made a mistake in your code that results in undefined behavior. And “undefined” implies “it might work under some circumstances”.
Why would inserting a
printfmake it work? Some possibilities:It changes the timing relationships between different parts of your program, or between your program and the rest of the world.
The printf call inhibits a compiler optimization that might otherwise take place.
The printf call changes the internal state of the standard library in a way that prevents the bug from occurring.
The printf call interacts with the hardware in a way that prevents the bug from occurring.
(Note that many of the same considerations are also true when running under a debugger — thus the term “heisenbug”: a bug that only occurs when you’re not watching for it.)