【发布时间】:2020-02-08 03:35:23
【问题描述】:
在我的代码中,我有一个名为membrane 的类,一个名为exciteMod() 的函数,一个名为decide() 的函数和一个名为delta_U 的变量。 exciteMod() 的第一行是this->delta_U = 0。在decide() 中,我的指数为-delta_U (exp(-this->delta_U))。这会导致错误使用大小为 8 的未初始化值。什么可能导致这种情况?我对 valgrind 中生成的 delta_U 没有任何错误。
编辑: 以下是相关代码段:
void membrane::exciteMod(){
this->delta_U = 0;
/* Do some stuff which does not directly affect this->delta_U*/
std::tr1::shared_ptr<bead> bit = this->beads.begin();
while (bit != this->nead.end()){
std::tr1::shared_ptr<bead> b = *bit++;
//calculate the doubles U and nextU on b, nothing here gives a warning in valgrind, anyhow U and nextU on b are always defined
this->delta_U += (b->nextU - b->U);
}
decide();
}
void membrane::decide(){
double r = P.r.ran3() // the random function from numerical recepies
double f = - this->delta_U;
if (r > exp(f)){ //this gives the warning even though delta_U is valid
/*stuff*/
}
}
这是警告:
==467== 使用大小为 8 的未初始化值
==467== 在 0x300B00D75D:__ieee754_exp(在 /lib64/libm-2.5.so 中)
==467== by 0x300B022FA3: exp (in /lib64/libm-2.5.so)
==467== by 0x40BB9A:membrane::decide() (membrane.cpp:813)
==467== by 0x40EBB1:membrane::exciteMod() (membrane.cpp:639)
==467== by 0x413994:membrane::MCstep(int) (membrane.cpp:486)
==467== by 0x402767: main (main.cpp:14)
编辑:
我应该提到我打电话给decide()的唯一地方是exciteMod()内部。
【问题讨论】:
-
顺便说一句,您不需要
this->语法来访问成员函数中的成员变量。直接访问它们:delta_u = 0;