c – 为什么这会使用大小为8的未初始化值

在我的代码中,我有一个名为membrane的类,其函数名为exciteMod(),一个名为determine()的函数和一个名为delta_U的变量. exciteMod()的第一行是this-> delta_U = 0.在decision()中,我有一个指数-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== Use of uninitialised value of size 8
==467== at 0x300B00D75D: __ieee754_exp (in /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)

编辑:
我应该提到我调用decision()的唯一地方是exciteMod().

最佳答案
未初始化值的最可能原因是您添加到delta_U的b-> nextU或b-> U中的至少一个本身未初始化.那是:

foo = 0;
foo += some_uninitialized_value;
if (foo)  // Valgrind warns here

你希望Valgrind在foo变得未初始化时报告.不幸的是,这样做会产生太多“误报”警告,这些警告是切实可行的.

您可以在循环中插入对VALGRIND_CHECK_MEM_IS_DEFINED的调用(参见Valgrind user manual),Valgrind将发出delta_U未定义的确切时刻.

转载注明原文:c – 为什么这会使用大小为8的未初始化值 - 代码日志