内存检测工具valgrind的安装和简单使用

1. 安装

tar -jxvf valgrind-3.13.0.tar.bz2

cd valgrind-3.13.0

sudo ./configure

sudo make

sudo make install

2. 简单使用

#include <stdio.h>
#include <stdlib.h>

void f()
{
    int *x = malloc(10 * sizeof(int));
    x[10] = 0;
}

int main()
{
    f();

    return 0;
}
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
==16723== Memcheck, a memory error detector
==16723== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==16723== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==16723== Command: ./main.out
==16723== 
==16723== Invalid write of size 4
==16723==    at 0x108668: f (val.c:7)
==16723==    by 0x10867E: main (val.c:12)
==16723==  Address 0x5857068 is 0 bytes after a block of size 40 alloc'd
==16723==    at 0x4C2FB6B: malloc (vg_replace_malloc.c:299)
==16723==    by 0x10865B: f (val.c:6)
==16723==    by 0x10867E: main (val.c:12)
==16723== 
==16723== 
==16723== HEAP SUMMARY:
==16723==     in use at exit: 40 bytes in 1 blocks
==16723==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==16723== 
==16723== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==16723==    at 0x4C2FB6B: malloc (vg_replace_malloc.c:299)
==16723==    by 0x10865B: f (val.c:6)
==16723==    by 0x10867E: main (val.c:12)
==16723== 
==16723== LEAK SUMMARY:
==16723==    definitely lost: 40 bytes in 1 blocks
==16723==    indirectly lost: 0 bytes in 0 blocks
==16723==      possibly lost: 0 bytes in 0 blocks
==16723==    still reachable: 0 bytes in 0 blocks
==16723==         suppressed: 0 bytes in 0 blocks
==16723== 
==16723== For counts of detected and suppressed errors, rerun with: -v
==16723== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

3. 进阶

待续

猜你喜欢

转载自www.cnblogs.com/douzujun/p/10827843.html