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

Результаты записываются в файл dm-log следующим образом:

$ cat dm-log

1062078174: 1: Dmalloc version '4.8.1' from 'http://dmalloc.com/'

1062078174: 1: flags = 0x4e40503, logfile 'dm-log'

1062078174: 1: interval = 100, addr = 0, seen # = 0

1062078174: 1: starting time = 1062078174

1062078174: 1: free bucket count/bits: 63/6

1062078174: 1: basic-block 4096 bytes, alignment 8 bytes, heap grows up

1062078174: 1: heap: 0x804a000 to 0x804d000, size 12288 bytes (3 blocks)

1062078174: 1: heap checked 0

1062078174: 1: alloc calls: malloc 1, calloc 0, realloc 0, free 0

1062078174: 1: alloc calls: recalloc 0, memalign 0, valloc 0

1062078174: 1: total memory allocated: 30 bytes (1 pnts)

1062078174: 1: max in use at one time: 30 bytes (1 pnts)

1062078174: 1: max alloced with 1 call: 30 bytes

1062078174: 1: max alloc rounding loss: 34 bytes (53%)

1062078174: 1: max memory space wasted: 3998 bytes (98%)

1062078174: 1: final user memory space: basic 0, divided 1, 4062 bytes

1062078174: 1: final admin overhead: basic 1, divided 1, 8192 bytes (66%)

1062078174: 1: final external space: 0 bytes (0 blocks)

1062078174: 1: top 10 allocations:

1062078174: 1: total-size count in-use-size count source

1062078174: 1:         30     1          30     1 ra=0x8048412

1062078174: 1:         30     1          30     1 Total of 1

1062078174: 1: dumping not-freed pointers changed since 0:

1062078174: 1: not freed: '0x804c008|s1' (30 bytes) from 'ra=0x8048412'

1062078174: 1: total-size count source

1062078174: 1:         30     1 ra=0x8048412 /* Выделение здесь */

1062078174: 1:         30     1 Total of 1

1062078174: 1: unknown memory: 1 pointer, 30 bytes

1062078174: 1: ending time = 1062078174, elapsed since start = 0:00:00

Вывод содержит много статистических данных, которые нам пока не интересны. Интересна строка, в которой указывается не освобожденная память, с адресом возврата, указывающим на выделившую память функцию ('ra=0х8048412'). Документация dmalloc объясняет, как получить расположение в исходном коде этого адреса с использованием GDB.

$ gdb ch15-badmem1 /* Запустить GDB */

GNU gdb 5.3

...

(gdb) x 0x8048412 /* Проверить адрес */

0x8048412 : 0х8910с483

(gdb) info line *(0x8048412) /* Получить сведения о строке */

Line 11 of "ch15-badmem1.с" starts at address 0x8048408

and ends at 0x8048418 .

Это трудно, но выполнимо, если нет другого выбора. Однако, если вы включите в свою программу заголовочный файл "dmalloc.h" (после всех остальных операторов #include), вы можете получить сведения из исходного кода непосредственно в отчете.

...

1062080258: 1: top 10 allocations:

1062080258: 1: total-size count in-use-size count source

1062080258: 1:        30      1          30     1 ch15-badmem2.c:13

1062080258: 1:        30      1          30     1 Total of 1

1062080258: 1: dumping not-freed pointers changed since 0:

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

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

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

Стивен Прата

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