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

4  ==8716== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.

5  ==8716== Estimated CPU clock rate is 2400 MHz

6  ==8716== For more details, rerun with: -v

7  ==8716==

8  p =

9  ==8716== Invalid write of size 1

10 ==8716== at 0x8048466: main (ch15-badmem1.c:18)

11 ==8716== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)

12 ==8716== by 0x8048368: (within /home/arnold/progex/code/ch15/ch15-badmem1)

13 ==8716== Address 0x4104804E is 12 bytes after a block of size 30 alloc'd

14 ==8716== at 0x40025488: malloc (vg_replace_malloc.с:153)

15 ==8716== by 0x8048411: main (ch15-badmem1.c:11)

16 ==8716== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)

17 ==8716== by 0x8048368: (within /home/arnold/progex/code/ch15/ch15-badmem1)

18 ==8716==

19 ==8716== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

20 ==8716== malloc/free: in use at exit: 30 bytes in 1 blocks.

21 ==8716== malloc/free: 1 allocs, 0 frees, 30 bytes allocated.

22 ==8716== For a detailed leak analysis, rerun with: --leak-check=yes

23 ==8716== For counts of detected errors, rerun with: -v

(Были добавлены номера строк в выводе, чтобы облегчить обсуждение.) Строка 8 является выводом программы; остальные от Valgrind в стандартную ошибку. Сообщение об ошибке находится в строках 9–17. Она указывает, сколько байтов было записано неверно (строка 9), где это случилось (строка 10), и показывает трассировку стека. Строки 13–17 описывают, откуда была выделена память. Строки 19–23 подводят итоги.

Опция -f программы ch15-badmem1 освобождает выделенную память, а затем записывает в нее через висячий указатель. Вот что сообщает Valgrind в этом случае:

$ valgrind ch15-badmem1 -f

==8719== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.

...

p =

==8719== Invalid write of size 1

==8719== at 0x8048498: main (ch15-badmem1.с:21)

==8719== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)

==8719== by 0x8048368: (within /home/arnold/progex/code/ch15/ch15-badmem1)

==8719== Address 0x41048024 is 0 bytes inside a block of size 30 free'd

==8719== at 0x40025722: free (vg_replace_malloc.с:220)

==8719== by 0x8048491: main (ch15-badmem1.c:20)

==8719== by 0x420158D3: __libc_start_main (in /lib/i686/libc-2.2.93.so)

==8719== by 0x8048368: (within /home/arnold/progex/code/ch15/ch15-badmem1)

...

На этот раз в отчете указано, что запись была осуществлена в освобожденную память и что вызов free() находится в строке 20 ch15-badmem1.c.

При вызове без опций ch15-badmem1.c выделяет и использует память, но не освобождает ее. О таком случае сообщает опция —leak-check=yes:

$ valgrind --leak-check=yes ch15-badmem1

1  ==8720== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.

...

8  p =

9  ==8720==

10 ==8720== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

11 ==8720== malloc/free: in use at exit: 30 bytes in 1 blocks.

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

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

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

Стивен Прата

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