Valgrind错误:在退出时使用:72,704字节C++初始化列表奇怪与char*

use*_*057 7 c++ initializer-list initialization-list

问题:

我有一个奇怪的问题,我没想到.我有一个名为Answers的类,在标题内是这样的:

class Answer
{
    char* aText;
    bool b_correct;
public:
    Answer():aText(0){;}  //default constructor
}
Run Code Online (Sandbox Code Playgroud)

主(测试)驱动程序代码是这样的:

int main(void) 
{

    static const unsigned int MAX_ANSWERS = 5;
    Answer answers[MAX_ANSWERS];
}
Run Code Online (Sandbox Code Playgroud)

我得到的(意外)怪异是发生了一个分配,我还没有在我的代码中使用任何新的东西.我猜测char*在初始化列表中调用它.

我使用valgrind来测试我的代码,我得到11个allocs和10个frees.当我删除初始化器时:aText(0),额外的alloc消失了.

我知道这是构造糟糕的代码.我正在学习如何用C++编写课程大纲.有人可以帮我理解内存的分配方式或初始化列表中发生的事情,以便调用新的内容吗?

我知道错误来自显示的代码.我知道额外的alloc正在发生当我编译并运行这个代码时.

Valgrind输出:

==12598== Memcheck, a memory error detector
==12598== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12598== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==12598== Command: ./Answers
==12598== 
==12598== 
==12598== HEAP SUMMARY:
==12598==     in use at exit: 72,704 bytes in 1 blocks
==12598==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==12598== 
==12598== LEAK SUMMARY:
==12598==    definitely lost: 0 bytes in 0 blocks
==12598==    indirectly lost: 0 bytes in 0 blocks
==12598==      possibly lost: 0 bytes in 0 blocks
==12598==    still reachable: 72,704 bytes in 1 blocks
==12598==         suppressed: 0 bytes in 0 blocks
==12598== Rerun with --leak-check=full to see details of leaked memory
==12598== 
==12598== For counts of detected and suppressed errors, rerun with: -v
==12598== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Run Code Online (Sandbox Code Playgroud)

平台信息:

Fedora 22

gcc.x86_64 5.1.1-4.fc22

valgrind.x86_64 1:3.10.1-13.fc22

codeblocks.x86_64 13.12-14.fc22

use*_*057 13

这是一个已知的GCC 5.1错误,而不是valgrind错误.

详情请访问:https: //gcc.gnu.org/bugzilla/show_bug.cgi?id = 64535

可能的解决方法:将 GCC降级到早期版本或等待Valgrind更新此错误的修复程序.这两种解决方案正在由各自的社区进行.

  • Bug仍然存在于5.4中 (5认同)
  • 正确的答案可能是:/sf/ask/2126362101/ (2认同)