C++ 为什么这会给出一个 Use of uninitialised value of size 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7098253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:28:06  来源:igfitidea点击:

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

c++valgrind

提问by Yotam

In my code I have a class named membranewith 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_Uwhich is generated in valgrind.

在我的代码中,我有一个名为 的类membrane,一个名为exciteMod()的函数,一个名为 的函数decide()和一个名为 的变量delta_U。的第一行exciteMod()this->delta_U = 0。在decide()我有一个-delta_U( exp(-this->delta_U))的指数。这会导致错误 使用大小为 8 的未初始化值。什么可能导致这种情况?我没有任何关于delta_Uvalgrind 生成的错误。

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)

==467==
在 0x300B00D75D 处使用大小为 8 的未初始化值==467==:__ieee754_exp(在 /lib64/libm-2.5.so 中)
==467== by 0x300B022FA3:exp(在 /lib64/libm-2.5.so 中)所以)
==467== by 0x40BB9A:membrane::decide() (membrane.cpp:813)
==467== by 0x40EBB1:membrane::exciteMod() (membrane.cpp:639)
==467== 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().

编辑:
我应该提到我打电话的唯一地方decide()exciteMod().

回答by Employed Russian

The most likely cause of uninitialized value is that at least one of b->nextUor b->Uthat you are adding to delta_Uis itself uninitialized. That is:

未初始化值的最可能原因是至少其中之一b->nextUb->U您添加的delta_U值本身未初始化。那是:

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.

您希望 Valgrind 在 foo 未初始化时进行报告。不幸的是,这样做会产生太多的“误报”警告,不切实际。

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_Ubecomes undefined.

您可以插入到循环调用中VALGRIND_CHECK_MEM_IS_DEFINED(请参阅Valgrind 用户手册),Valgrind 会在delta_U未定义时发出确切的信号。

回答by Hemjal

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

假设您需要存储 t 个值。然后使用这个:

int *p = (int *) malloc(t*(sizeof(int)+1));
memset(p,0,t*(sizeof(int)+1));