Why does removing 'cout' from my function change its result? [closed]

允我心安 提交于 2021-02-16 04:09:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!