valgrind - a suite of tools for debugging and profiling programs

本文介绍使用Valgrind工具进行内存泄漏检测的方法。Valgrind是一款强大的调试和剖析工具,能够帮助开发者发现程序中的内存泄漏问题。文章通过一个简单的C语言示例,展示了如何利用Valgrind定位和修复内存泄漏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VALGRIND(1)                                                                   Release 3.7.0                                                                   VALGRIND(1)

NAME
       valgrind - a suite of tools for debugging and profiling programs

SYNOPSIS
       valgrind [valgrind-options] [your-program] [your-program-options]

DESCRIPTION
       Valgrind is a flexible program for debugging and profiling Linux executables. It consists of a core, which provides a synthetic CPU in software, and a series of
       debugging and profiling tools. The architecture is modular, so that new tools can be created easily and without disturbing the existing structure.

       Some of the options described below work with all Valgrind tools, and some only work with a few or one. The section MEMCHECK OPTIONS and those below it describe
       tool-specific options.

       This manual page covers only basic usage and options. For more comprehensive information, please see the HTML documentation on your system:
       $INSTALL/share/doc/valgrind/html/index.html, or online: http://www.valgrind.org/docs/manual/index.html.


Example:

wgao@pek-wgao-d1:~$ cat leak.c
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

int main()
{
    int * x = NULL;
    /*int a[10] = {0};
    x = a;
    *x = 1;
    printf ("%d\n",*x);
    x[9] = 0;
    printf ("%d\n",*x);
    */
    x = malloc (10 * sizeof(int));
    memset(x,0,10);
    *x = 1;
    printf ("%d\n",*x);
    x[9] = 0;
    #free(x);
}

wgao@pek-wgao-d1:~$
wgao@pek-wgao-d1:~$ gcc -o leak -O0 -g leak.c 

wgao@pek-wgao-d1:~$ sudo valgrind --leak-check=full -v ./leak
==14363== HEAP SUMMARY:
==14363==     in use at exit: 40 bytes in 1 blocks
==14363==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==14363==
==14363== Searching for pointers to 1 not-freed blocks
==14363== Checked 72,456 bytes
==14363==
==14363== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==14363==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14363==    by 0x40055D: main (leak.c:15)
==14363==
==14363== LEAK SUMMARY:
==14363==    definitely lost: 40 bytes in 1 blocks
==14363==    indirectly lost: 0 bytes in 0 blocks
==14363==      possibly lost: 0 bytes in 0 blocks
==14363==    still reachable: 0 bytes in 0 blocks
==14363==         suppressed: 0 bytes in 0 blocks
==14363==
==14363== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--14363--
--14363-- used_suppression:      2 dl-hack3-cond-1
==14363==
==14363== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
wgao@pek-wgao-d1:~$






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值