0
#include <bits/stdc++.h>
int main(){
    int a, b = 10, r;
    printf("%d\n", a);
}

==13235== Memcheck, a memory error detector
==13235== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13235== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==13235== Command: ./assign
==13235==
==13235== error calling PR_SET_PTRACER, vgdb might block
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4AAEAD8: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Use of uninitialised value of size 8
==13235==    at 0x4A9281B: _itoa_word (_itoa.c:179)
==13235==    by 0x4AAE6F4: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4A9282D: _itoa_word (_itoa.c:179)
==13235==    by 0x4AAE6F4: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4AAF3A8: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235==
==13235== Conditional jump or move depends on uninitialised value(s)
==13235==    at 0x4AAE86E: __vfprintf_internal (vfprintf-internal.c:1687)
==13235==    by 0x4A98EBE: printf (printf.c:33)
==13235==    by 0x1091B1: main (assignment.cpp:5)
==13235== 0
==13235==
==13235== HEAP SUMMARY:
==13235==     in use at exit: 0 bytes in 0 blocks
==13235==   total heap usage: 2 allocs, 2 frees, 73,216 bytes allocated
==13235==
==13235== All heap blocks were freed -- no leaks are possible
==13235==
==13235== Use --track-origins=yes to see where uninitialised values come from
==13235== For lists of detected and suppressed errors, rerun with: -s
==13235== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

我无法从此输入中出错,显示什么堆摘要以及为什么未初始化变量的 size=8?另外,“总堆使用量:2 个分配,2 个空闲,73,216 个字节分配”这代表什么?“错误摘要:来自 5 个上下文的 5 个错误(抑制:0 来自 0)”?

4

2 回答 2

1

未初始化的变量错误来自这一行:

printf("%d\n", a);

因为您使用a时没有给它一个初始值。这会调用未定义的行为,并且永远不应该这样做。

变量的大小为 8 字节表明您正在 64 位架构上编译,其中 anint是 8 字节或 64 位。

总堆使用量只是在程序执行期间分配和释放的所有内存的汇总。由于最后使用了 0 个字节,这意味着您的程序没有泄漏任何内存。

于 2020-05-23T15:28:37.433 回答
1

来自 valgrind 的第一条消息指出“条件跳转或移动取决于未初始化的值”并在您的代码中引用此行:

printf("%d\n", a);

此行使用变量a. 如果你再看前一行:

int a, b = 10, r;

你会看到它a从未被赋值。也就是说,它是未初始化的。这就是 valgrind 所抱怨的。你可以通过给出a一个值来解决这个问题:

int a = 1, b = 10, r;

其余的 valgrind 消息引用文件中的同一行代码,因此其余错误从第一个错误开始,修复一个应该修复其余错误。

于 2020-05-23T15:29:05.417 回答