Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would this give a Use of uninitialised value of size 8

Tags:

c++

valgrind

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().

like image 818
Yotam Avatar asked Aug 17 '11 19:08

Yotam


1 Answers

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.

like image 163
Employed Russian Avatar answered Sep 23 '22 09:09

Employed Russian