1. 安装
tar -jxvf valgrind-3.13..tar.bz2cd valgrind-3.13.sudo ./configuresudo makesudo make install
2. 简单使用
#include <stdio.h>
#include <stdlib.h>void f()
{
int *x = malloc( * sizeof(int));
x[] = ;
}int main()
{
f(); return ;
}
gcc -g val.c -o main.out
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./main.out
--leak-check=yes --show-reachable=yes ./main.out
==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.13. and LibVEX; rerun with -h for copyright info
==== Command: ./main.out
====
==== Invalid write of size
==== at 0x108668: f (val.c:)
==== by 0x10867E: main (val.c:)
==== Address 0x5857068 is bytes after a block of size alloc'd
==== at 0x4C2FB6B: malloc (vg_replace_malloc.c:)
==== by 0x10865B: f (val.c:)
==== by 0x10867E: main (val.c:)
====
====
==== HEAP SUMMARY:
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, bytes allocated
====
==== bytes in blocks are definitely lost in loss record of
==== at 0x4C2FB6B: malloc (vg_replace_malloc.c:)
==== by 0x10865B: f (val.c:)
==== by 0x10867E: main (val.c:)
====
==== LEAK SUMMARY:
==== definitely lost: bytes in blocks
==== indirectly lost: bytes in blocks
==== possibly lost: bytes in blocks
==== still reachable: bytes in blocks
==== suppressed: bytes in blocks
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )
3. 进阶
待续