Читаем Linux программирование в примерах полностью

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

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных