我可以让valgrind告诉我_哪些_值未初始化吗? [英] Can I get valgrind to tell me _which_ value is uninitialized?

查看:163
本文介绍了我可以让valgrind告诉我_哪些_值未初始化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些代码上运行了valgrind,如下所示:

I ran valgrind on some code as follows:

valgrind --tool=memcheck --leak-check=full --track-origins=yes ./test

它返回以下错误:

==24860== Conditional jump or move depends on uninitialised value(s)
==24860==    at 0x4081AF: GG::fl(M const&, M const&) const (po.cpp:71)
==24860==    by 0x405CDB: MO::fle(M const&, M const&) const (m.cpp:708)
==24860==    by 0x404310: M::operator>=(M const&) const (m.cpp:384)
==24860==    by 0x404336: M::operator<(M const&) const (m.cpp:386)
==24860==    by 0x4021FD: main (test.cpp:62)
==24860==  Uninitialised value was created by a heap allocation
==24860==    at 0x4C2EBAB: malloc (vg_replace_malloc.c:299)
==24860==    by 0x40653F: GODA<unsigned int>::allocate_new_block() (goda.hpp:82)
==24860==    by 0x406182: GODA<unsigned int>::GODA(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) (goda.hpp:103)
==24860==    by 0x402A0E: M::init(unsigned long) (m.cpp:63)
==24860==    by 0x403831: M::M(std::initializer_list<unsigned int>, MO const*) (m.cpp:248)
==24860==    by 0x401B56: main (test.cpp:31)

因此第71行有错误.好,很好.以下是通往po.cpp的71行的行(71行最后):

So line 71 has an error. OK, great. Here are the lines leading up to line 71 of po.cpp (line 71 is last):

DEG_TYPE dtk = t.ord_deg();
DEG_TYPE duk = u.ord_deg();
bool searching = dtk == duk;
NVAR_TYPE n = t.nv();
NVAR_TYPE k = 0;
for (/* */; searching and k < n; ++k) { // this is line 71

好,那么第71行的哪个值未初始化?

OK, so which value of line 71 is uninitialized?

  • 肯定不是k;
  • 我手动检查(=逐步通过gdb")t的构造函数初始化了t.nv()返回的值,所以肯定不是n(实际上n设置为6) ,正确的值);
  • searchingdtkduk确定,但我还手动检查了tu的构造函数是否初始化了.ord_deg()返回的值(实际上,两个dtkduk设置为3(正确的值).
  • certainly not k;
  • I manually checked (= "stepping through gdb") that t's constructor initializes the value that is returned by t.nv(), so certainly not n (in fact n is set to 6, the correct value);
  • searching is determined by dtk and duk, but I also manually checked that t's and u's constructors initialize the values that are returned by .ord_deg() (in fact both dtk and duk are set to 3, the correct value).

我在这里完全不知所措.是否有一些选项可以告诉valgrind报告哪个精确值它认为未初始化?

I'm at a complete loss here. Is there some option that will tell valgrind to report which precise value it thinks is uninitialized?

更新

为回答一个问题,这是test.cpp的第61行:

In answer to one question, here is line 61 of test.cpp:

M s { 1, 0, 5, 2, 0 };

因此它使用初始化列表进行构造.这是构造函数:

So it constructs using an initializer list. Here's that constructor:

M::M(
    initializer_list<EXP_TYPE> p, const MO * ord
) {
  common_init(ord);
  init_e(p.size());
  NVAR_TYPE i = 0;
  last = 0;
  for (
       auto pi = p.begin();
       pi != p.end();
       ++pi
  ) {
    if (*pi != 0) {
      e[last] = i;
      e[last + 1] = *pi;
      last += 2;
    }
    ++i;
  }
  ord->set_data(*this);
}

这是类中的数据,添加注释以显示其初始化位置:

Here's the data in the class, adding comments showing where it's initialized:

NVAR_TYPE n;    // init_e()
EXP_TYPE * e;   // common_init()
NVAR_TYPE last; // common_init()
DEG_TYPE od;    // common_init(), revised in ord->set_data()
const MO * o;   // common_init()
MOD * o_data;   // common_init(), revised in ord->set_data()

推荐答案

是否有一些选项可以告诉valgrind报告精确度 它认为未初始化的值?

Is there some option that will tell valgrind to report which precise value it thinks is uninitialized?

最好的办法是使用--track-origins=yes(您已经在使用此选项). Valgrind将仅告诉您未初始化值的近似位置(以Valgrind表示),而不告诉您确切的变量名称.有关--track-origins,请参见 Valgrind手册:

The best you can do is to use --track-origins=yes (you already using this option). Valgrind will tell you only approximate location of uninitialised values (origin in terms of Valgrind), but not exact variable name. See Valgrind manual for --track-origins:

设置为是"时,Memcheck会跟踪所有 未初始化的值.然后,当未初始化的值错误是 报告,Memcheck将尝试显示该值的来源.起源 可以是以下四个位置之一:堆块,堆栈 分配,客户请求或其他各种来源(例如, 致电brk).

When set to yes, Memcheck keeps track of the origins of all uninitialised values. Then, when an uninitialised value error is reported, Memcheck will try to show the origin of the value. An origin can be one of the following four places: a heap block, a stack allocation, a client request, or miscellaneous other sources (eg, a call to brk).

对于源自堆块的未初始化值,Memcheck显示 分配块的位置.对于原始的未初始化值 从堆栈分配中,Memcheck可以告诉您哪个函数 分配了值,但仅此而已-通常它会向您显示 该函数的右括号的源位置.那么你 应该仔细检查函数的所有局部变量是否 正确初始化.

For uninitialised values originating from a heap block, Memcheck shows where the block was allocated. For uninitialised values originating from a stack allocation, Memcheck can tell you which function allocated the value, but no more than that -- typically it shows you the source location of the opening brace of the function. So you should carefully check that all of the function's local variables are initialised properly.

这篇关于我可以让valgrind告诉我_哪些_值未初始化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆