c毕业设计外文翻译--修正内存问题(编辑修改稿)内容摘要:
l is Purify, released in 1991 by Pure Software. Purify’s name has since bee synonymous with memory debugging. There is also Insure++, Valgrind, and BoundsChecker, among others. See the tools Appendix starting on page 198 for references and the survey in [Luecke06] for a parison of features. Memory debuggers do detailed bookkeeping of all allocated/deallocated dynamic memory. They also intercept and check access to dynamic memory. Some memory debuggers can check access to local variables on the stack and statically allocated memory. Purify and BoundsChecker do this by object code instrumentation at program link time, Insure++ uses source code instrumentation, and Valgrind executes the program on a virtual machine and monitors all memory transactions. The code instrumentation allows the tools to pinpoint the source code statement where a memory bug occurred. The following bugs are detectable by a memory debugger: • Memory leaks • Accessing memory that was already freed • Freeing the same memory location more than once • Freeing memory that was never allocated • Mixing C malloc()/free()with C++ new/delete • Using delete instead of delete[] for arrays • Array outofbound errors • Accessing memory that was never allocated • Uninitialized memory read • Null pointer read or write We will show in the next section how to attach a memory debugger to your program, and how the tool finds and reports bugs. 36 4 Fixing Memory Problems Example 1: Detecting Memory Access Errors Our first example is a program that allocates an array in dynamic memory, accesses an element outside the final array element, reads an uninitialized array element, and finally forgets to deallocate the array. We use the public domain tool Valgrind on Linux as the memory debugger, and demonstrate how the tool automatically detects these bugs. This is the code of our program : 1 /* */ 2 include 3 int main(int argc, char* argv[]) { 4 const int size=100。 5 int n, sum=0。 6 int* A = (int*)malloc( sizeof(int)*size )。 7 8 for (n=size。 n0。 n) /* walk through A[100]...A[1] */ 9 A[n] = n。 /* error: A[100] invalid write*/ 10 for (n=0。 nsize。 n++) /* walk through A[0]...A[99] */ 11 sum += A[n]。 /* error: A[0] not initialized*/ 12 printf (sum=%d\n, sum)。 13 return 0。 /* mem leak: A[] */ 14 } We pile the program with debug information and then run under Valgrind: gcc g valgrind tool=memcheck leakcheck=yes ./ In the f。c毕业设计外文翻译--修正内存问题(编辑修改稿)
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。