Why would this give a Use of uninitialised value o

2019-04-28 18:10发布

In my code I have a class named membrane with a function named exciteMod(), a function named decide() and a variable named delta_U. The first line of exciteMod() is this->delta_U = 0. In decide() I have an exponent of -delta_U (exp(-this->delta_U)). which cause an error Use of uninitialised value of size 8. What might cause this? I don't have any error about delta_U which is generated in valgrind.

Edit: Here are the relevant segment of the code:

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*/
  }
}

This is the warning:

==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)

Edit:
I should have mentioned that the only place where I call decide() is inside of exciteMod().

标签: c++ valgrind
1条回答
你好瞎i
2楼-- · 2019-04-28 18:44

The most likely cause of uninitialized value is that at least one of b->nextU or b->U that you are adding to delta_U is itself uninitialized. That is:

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

You would like Valgrind to report when foo becomes uninitialized. Unfortunately, doing so produces too many "false positive" warnings to be practical.

You can insert into your loop calls to VALGRIND_CHECK_MEM_IS_DEFINED (see Valgrind user manual), and Valgrind will signal the exact moment when delta_U becomes undefined.

查看更多
登录 后发表回答