开发者

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

开发者 https://www.devze.com 2023-03-29 00:15 出处:网络
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

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 开发者_如何学Ccause 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)


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.


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));
0

精彩评论

暂无评论...
验证码 换一张
取 消