问题
I am an intermediate programmer, writing a program that's probably much to complicated for me. The programs aim is to construct certain 2-d arrays, and has a few different class objects that are communicating with each other in a not-so-simple way. In order to debug, I added a line of code to output where I was in the program (cout << "here";). The program now works, but if I remove the cout statement, the overall result of the entire program fails; the program does not follow the intended procedure to create the said 2-d arrays.
So my question is: What could possibly be happening? or rather, how would including a cout statement "fix" (or have any kind of impact on) the other surrounding code? It does not seem like a cout statement would have an impact on Buffer overflow, but feel free to educate me if so.
回答1:
Your program uses constructs that are defined to cause "undefined behavior." More concretely, the program probably reads uninitialized memory from the execution stack or writes beyond the boundaries of something stored on the stack. Calling functions, like printing something to cout, modifies the stack and can cause the program to behave differently if you have these kinds of bugs.
来源:https://stackoverflow.com/questions/17802349/why-does-removing-cout-from-my-function-change-its-result