Memory Debuggers

http://elinux.org/Memory_Debuggers


Memory Debuggers

Several tools exist for finding memory leaks or for reporting individual memory allocations of a program. These tools help analyze memory usage patterns, detect unbalanced allocations and frees, report buffer over- and under-runs, etc.

mtrace

mtrace is a builtin part of glibc which allows detection of memory leaks caused by unbalanced malloc/free calls. To use it, the program is modified to call mtrace() and muntrace() to start and stop tracing of allocations. A log file is created, which can then be scanned by the 'mtrace' Perl script. The 'mtrace' program lists only unbalanced allocations. If source is available it can show the source line where the problem occurred. mtrace can be used on both C and C++ programs.

See the mtrace wikipedia article for more information.

memwatch

memwatch is a program that not only detects malloc and free errors but also reads and writes beyond the allocated space (buffer over and under-runs). To use it, you modify the source to include the memwatch code, which provides replacements for malloc and free.

Some things that memwatch does not catch are writing to an address that has been freed and reading data from outside the allocated memory.

mpatrol

mpatrol appears to be like memwatch.

See http://mpatrol.sourceforge.net/

dmalloc

"The debug memory allocation or dmalloc library has been designed as a drop in replacement for the system's malloc, realloc, calloc, free and other memory management routines while providing powerful debugging facilities configurable at runtime. These facilities include such things as memory-leak tracking, fence-post write detection, file/line number reporting, and general logging of statistics."

This library can be used without modifying the existing program, and uses environment variables to control it's operation and set of issues to log.

It's home page is at: http://dmalloc.com/

See Cal Erickson's article (link below, page 2) for information about using this system.

dbgmem

dbgmem looks like another dynamic library replacement tool, similar to dmalloc (but possibly having less features)

See http://dbgmem.sourceforge.net/

valgrind

valgrind does dynamic binary instrumentation to analyze the program, and provides a number of memory problem detection tools and profiling tools. Unfortunately, as of July 2010 it is only available for x86 and ppc64 architecture platforms.

See Valgrind

Electric Fence

See Electric Fence

Tutorials or Overviews


1. Are there any memory errors in the following programs? If so, list all of them. Assume that the user enters in correct input, and that the sizes entered are at least one. Write your solution in a text or Word file and submit it below. void main() { char *str, *input; int *ilist; int i, size1, size2; printf("Number of letters in word: "); scanf("%d", &size1;); /* user inputs an integer */ printf("Number of integers: "); scanf("%d", &size2;); /* user inputs an integer */ str = (char *) malloc(size1); ilist = (int *) malloc(size2); printf("Word: "); scanf("%s", str); /* user inputs a string */ for(i = 0; i < size2; i++) { printf("Number %d of %d: ", i + 1, size2); scanf("%d", ilist + i); /* user inputs an integer */ } } 2. Are there any memory errors in the following program? If so, list all of them. Write your solution in a text or Word file and submit it below. /* return 1 if str is "1", 0 otherwise */ int checkIf1(char *str) { char *newstr = malloc(strlen(str) + 1); strcpy(newstr, str); /* set newstr to str */ if (strcmp(newstr, "1") == 0) { /* newstr is "1" */ return 1; } free(newstr); return 0; } void main() { char *strArr[4] = {"1", "2", "3", "4"}; int i; for(i = 0; i < 4; i++) { printf("%d\n", checkIf1(strArr[i])); } } 3. Are there any memory errors in the following program? If so, list all of them. Write your solution in a text or Word file and submit it below. struct data { char *str1, *str2; }; /* returns two strings concatenated if they are not the same, NULL otherwise */ char *mergeSingleIfDifferent(char *s1, char *s2) { char *str = (char *) malloc(strlen(s1) + strlen(s2) + 1); if (strcmp(s1, s2) == 0) { /* strings are equal */ str = NULL; } else { strcpy(str, s1); strcat(str, s2); } return str; } /* copies merged strings (or NULL) into array of strings passed in (results) */ void mergeArrayIfDifferent(char *results[], char *strA1[], char *strA2[], int size) { int i; for(i = 0; i < size; i++) { results[i] = mergeSingleIfDifferent(strA1[i], strA2[i]); } } void printAndFree(int c, char *str) { if (str != NULL) { printf("%d: %s\n", c, str); free(str); } } void main() { char *strArr1[8] = {"1", "2", "3", "4", "5", "6", "7", "8"}; char *strArr2[8] = {"a", "2", "c", "4", "e", "6", "g", "8"}; char *results[8]; int i; mergeArrayIfDifferent(results, strArr1, strArr2, 8); for(i = 0; i < 8; i++) { printAndFree(i, results); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值