12 ==8720== malloc/free: 1 allocs, 0 frees, 30 bytes allocated.
...
16 ==8720==
17 ==8720== 30 bytes in 1 blocks are definitely lost in loss record 1 of 1
18 ==8720== at 0x40025488: malloc (vg_replace_malloc.c:153)
19 ==8720== by 0x8048411: main (ch15-badmem1.c:11)
20 ==8720== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)
21 ==8720== by 0x8048368: (within /home/arnold/progex/code/ch15/ch15-badmem1)
22 ==8720==
23 ==8720== LEAK SUMMARY:
24 ==8720== definitely lost: 30 bytes in 1 blocks.
25 ==8720== possibly lost: 0 bytes in 0 blocks.
26 ==8720== still reachable: 0 bytes in 0 blocks.
27 ==8720== suppressed: 0 bytes in 0 blocks.
28 ==8720== Reachable blocks (those to which a pointer was found) are not shown.
29 ==8720== To see them, rerun with: --show-reachable=yes
Строки 17–29 предоставляют отчет об утечке; эта память была выделена в строке 11 ch15-badmem1.с
.
Помимо отчетов о неправильном использовании динамической памяти, Valgrind может диагностировать использование ch15-badmem3.c
:
1 /* ch15-badmem3.c --- плохое обращение с нединамической памятью */
2
3 #include
4 #include
5
6 int main(int argc, char **argv)
7 {
8 int a_var; /* Обе не инициализированы */
9 int b_var;
10
11 /* Valgrind не отметит это; см. текст. */
12 a_var = b_var;
13
14 /* Использование неинициализированной памяти; это отмечается. */
15 printf("a_var = %d\n", a_var);
16
17 return 0;
18 }
При запуске Valgrind выдает этот (сокращенный) отчет:
==29650== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
...
==29650== Use of uninitialised value of size 4
==29650== at 0x42049D2A: _IO_vfprintf_internal (in /lib/i686/libc-2.2.93.so)
==29650== by 0x420523C1: _IO_printf (in /lib/1686/libc-2.2.93.so)
==29650== by 0x804834D: main (ch15-badmem3.с:15)
==29650== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)
==29650==
==29650== Conditional jump or move depends on uninitialised value(s)
==29650== at 0X42049D32: _IO_vfprintf_internal (in /lib/i686/libc-2.2.93.so)
==29650== by 0x420523C1: _IO_printf (in / lib/i686/libc-2.2.93.so)
==29650== by 0x804834D: main (ch15-badmem3.c:15)
==29650== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)
...
a_var = 1107341000
==29650==
==29650== ERROR SUMMARY: 25 errors from 7 contexts (suppressed: 0 from 0)
==29650== malloc/free: in use at exit: 0 bytes in 0 blocks.
==29650== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==29650== For a detailed leak analysis, rerun with: --leak-check=yes
==29650== For counts of detected errors, rerun with: -v