我该如何解释此错误,堆摘要,未初始化的变量?

#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)”?

评论
  • o梦倾城
    o梦倾城 回复

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

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

    since you are using a without giving it an initial value. This invokes undefined behavior, and should never be done.

    The size of the variable being 8 bytes suggests that you are compiling on a 64bit architecture, where an int is 8 bytes, or 64 bits.

    总堆使用情况只是程序执行期间已分配并释放的所有内存的摘要。由于最后使用的字节数为0,这意味着您的程序不会泄漏任何内存。

  • zamet
    zamet 回复

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

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

    This line uses the variable a. If you then look at the preceding line:

    int a, b = 10, r;
    

    You'll see that a is never assigned a value. That is, it is uninitialized. This is what valgrind is complaining about. You can fix this by giving a a value:

    int a = 1, b = 10, r;
    

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