7

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. The only place where I call decide() is inside of exciteMod(). What might cause this? I don't have any error about delta_U which is generated in valgrind.

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)
Elijah Mock
  • 587
  • 8
  • 21
Yotam
  • 10,295
  • 30
  • 88
  • 128
  • 4
    By the way, you don't need the `this->` syntax to access member variables within a member function. Access them directly: `delta_u = 0;` – Thomas Matthews Aug 17 '11 at 19:48

2 Answers2

15

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Yea I have found this late last night. What I don't understand is why this is the place the error is thrown and previously. – Yotam Aug 18 '11 at 06:30
0

lets say you have t number of values needed to be stored. then use this:

int *p = (int *) malloc(t*(sizeof(int)+1));
memset(p,0,t*(sizeof(int)+1));
Hemjal
  • 130
  • 2
  • 7