为什么打印一个变量会改变它的值?
Why would printing a variable change its value?
我有一个小函数,它应该是基于机器学习算法进行预测。这个函数不工作,所以我放了一条print语句来检查值,突然它开始工作了。当我注释掉打印行时,它又停止工作了。这一切发生的原因,我是不是漏掉了什么?
int makePrediction( const InstanceT & instance, bool biased ){
double dotProduct = ( biased ? instance * _weights + _bias : instance * _weights );
std::cout << "dotProduct = " << dotProduct << std::endl;
return ( dotProduct > 0 ? 1 : -1 );
}
由于某种原因产生了与
不同的结果int makePrediction( const InstanceT & instance, bool biased ){
double dotProduct = ( biased ? instance * _weights + _bias : instance * _weights );
return ( dotProduct > 0 ? 1 : -1 );
}
和显示相同输入的结果是不同的,我用
调用这个函数:std::vector<InstanceT> _instances = populate_data() //this works for both versions
for ( int i = 0; i < _instances.size(); i++ ){
std::cout << "prediction: " << makePrediction( _instances[i], true ) << std::endl;
}
任何想法吗?
这种情况经常发生,原因有两个:
- 并发问题。如果你的程序是多线程的,你可以用调试输出掩盖竞争条件。试试像helgrind这样的MT调试器。
- 破碎的堆栈。试着在你的程序上运行valgrind,看看它是否干净。
当然,这些都是非常通用的建议,但你必须更好地说明你的问题,以获得更好的建议:-)。
相关文章:
- 变量没有改变?通过向量的函数调用
- 在向量内更改变量的值不会改变其在向量外的值
- C++11:具有互斥锁的线程看到原子变量的值发生变化,尽管这是唯一可以改变它的代码
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 标准在哪里定义了可变变量可以改变
- C++11:可变的 lambda 似乎没有改变变量?
- printf() 似乎改变了一个变量
- 为什么我的变量在单独的线程中修改时没有改变?C++/boost
- 为什么 Player.x 和 Player.y 变量没有改变?
- C - 如何将各处的静态函数称为改变其私有变量
- 即使"friend class rect"在平方类中被评论,为什么它要改变矩形类的私有变量?
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- 全局变量的顺序会改变C /OpenGL的性能
- 为什么一个函数的末尾有常量这个词,即使它确实改变了一个变量
- 如何在C++中改变超类和子类的变量
- setRoomName不会改变变量roomName
- 不能改变变量的值
- 是否有一种方法可以改变变量在内存中的存储方式(位大小)
- Std::cout改变变量的值
- 使用引用改变变量的地址