【正文】
1B8 is 0 bytes after a block ==11323== of size 400 alloc’d ==11323== at 0x1B903F40: malloc ==11323== (in /usr/lib/valgrind/) ==11323== by 0x80484F2: main (:6) The string ==11323== refers to the process ID and is useful when Valgrind is checking multiple processes 1. The important piece of information is that an invalid 1 Valgrind will, per default, check only the first (parent) process that has been invoked. Use option tracechildren=yes to check all child processes as well. Example 1: Detecting Memory Access Errors 37 write occurs in line 9 of . There is also additional information revealing the address of the closest allocated memory block and how it was allocated. The memory debugger guesses that the invalid write in line 9 is related to this memory block. The guess is correct because both belong to the same array A. Note that Valgrind is able to catch an outofarraybounds errors only when the array is allocated as dynamic memory with malloc() or new. This is the case in the example with the statement in line 6: 6 int* A = (int*)malloc( sizeof(int)*size )。動態(tài)內(nèi)存的分配(眾所周知的堆內(nèi)存)在 C或 C++中是在程序員的控制之下的。 錯誤的內(nèi)存管 理用法 一 套 完整 BUG 集合 與錯誤聲明 對于內(nèi)存管理來說 :解除一個錯誤的內(nèi)存超過一次,在解除以后訪問內(nèi)存,或解除一個從分配過的阻塞內(nèi)存。鍵指針不是引起內(nèi)存溢出和嚴(yán)重問題的關(guān)鍵。他們建立內(nèi)存管理機(jī)制為一個被分配阻塞內(nèi)存利用不同的機(jī)制,并且確保是否每個阻塞內(nèi)存在最后都得到了分配。Purify 和 BoundsChecker 用代碼編譯工具完成這些工作在程序連接期間,Insure++利用源代碼編譯工具, Valgrind 則把程序先執(zhí)行在虛擬機(jī)上并且監(jiān)聽所有的內(nèi)存處理。一次報告一個無用的寫錯誤。它表現(xiàn)出 Valgrind 不能夠發(fā)現(xiàn)這樣的錯誤但是 Purity 可以捕獲到它們。內(nèi)存調(diào)試器猜測第 9行與內(nèi)存阻塞有關(guān)。我們用公共范圍工具Valgrind在 Linux上作為內(nèi)存調(diào)試器,演示內(nèi)存調(diào)試器如何自動的解決這些 BUG。也有 Insure++, Valgrind,也有 BoundsChecker,除此之外的一些其他同類工具。未定義的變量中包含不穩(wěn)定的值。 在內(nèi)存中令人討厭的事是內(nèi)存重載的時候 BUG 沒有被聲明。 如果內(nèi)存泄漏經(jīng)常發(fā)生或者十分巨大,最后你電腦的主要內(nèi)存將被耗盡。在一下的章節(jié),我們將描述幾個代表性的在 C或 C++程序中處理的內(nèi)容 BUG,介紹內(nèi)容調(diào)試器,并且介紹兩個內(nèi)存如何發(fā)現(xiàn) BUG 的例子。nsize。Fixing Memory Problems This chapter is about finding bugs in C/C++ programs with the help of a memory debugger. A memory debugger is a runtime tool designed to trace and detect bugs in C/C++ memory management and access. It does not replace a general debugger. In the following sections, we will describe the memory access bugs that typically occur in C/C++ programs, introduce memory debuggers, and show with two examples how these tools find bugs. We will then show how to run memory and source code debuggers together, how to deal with unwanted error messages by writing a suppression file, and what restrictions need to be considered. Memory Management in C/C++ – Powerful but Dangerous The C/C++ language is able to manage m