The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck.
2. Preparing your programCompile your program with -g to include debugging information so that Memcheck's error messages include exact line numbers. Using -O0 is also a good idea, if you can tolerate the slowdown.
3. Running your program under Memcheckvalgrind --leak-check=yes myprog arg1 arg2
输出信息格式为[进程号][描述信息]
常用的错误信息 ==4401== Invalid write of size 1
==4401== at 0x80484B6: lstrcat (strcat.c:9)
==4401== by 0x804851B: main (strcat.c:20)
==4401== Address 0x4027030 is 0 bytes after a block of size 8 alloc'd
==4401== at 0x4008E28: malloc (vg_replace_malloc.c:270)
==4401== by 0x80484E8: main (strcat.c:15)
char buff[8]={0};
strcpy(buff,"123456789");
==4576== HEAP SUMMARY:
==4576== in use at exit: 8 bytes in 1 blocks
==4576== total heap usage: 1 allocs, 0 frees, 8 bytes allocated
==4576==
==4576== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4576== at 0x4008E28: malloc (vg_replace_malloc.c:270)
==4576== by 0x80484B8: main (strcat.c:15)
==4610== HEAP SUMMARY:
==4610== in use at exit: 8 bytes in 1 blocks
==4610== total heap usage: 1 allocs, 0 frees, 8 bytes allocated