为什么从函数中删除"cout"会改变其结果? [英] Why does removing 'cout' from my function change its result?

查看:88
本文介绍了为什么从函数中删除"cout"会改变其结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是中级程序员,所以编写对我来说可能很复杂的程序.该程序的目的是构造某些二维数组,并具有一些不同的类对象,它们以不太简单的方式彼此通信.为了进行调试,我在程序中的输出位置添加了一行代码(cout<<"here";).该程序现在可以运行,但是如果我删除cout语句,则整个程序的整体结果将失败.该程序未按照预期的过程创建所述二维数组.

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.

所以我的问题是:可能会发生什么?或者更确切地说,如何将包含cout语句的内容修复"(或对其他周围的代码产生影响)?似乎cout语句不会对Buffer溢出产生影响,但是如果这样的话,请随时教我.

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.

推荐答案

您的程序使用了定义为导致未定义行为"的构造.更具体地说,程序可能会从执行堆栈中读取未初始化的内存,或者写入超出堆栈中存储内容的边界.调用函数(例如打印一些东西到cout)会修改堆栈,并且如果您遇到这些错误,可能会导致程序的行为有所不同.

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.

这篇关于为什么从函数中删除"cout"会改变其结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆