Valgrind的使用

检测未初始化内存

例如:
创建一个test.c 文件 ,写一段代码 ,检测未初始化内存

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

int main(void)
{
    char *p;
    char c = *p;

    printf("\n [%c]\n", c);

    return 0;
}

在linux系统下 ,将test.c文件变成进程

gcc  test.c -o test

valgrind 命令:

valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./your_program
[cxq@iZwz9fjj2ssnshikw14avaZ ~]$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./test
==11958== Memcheck, a memory error detector
==11958== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11958== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==11958== Command: ./test
==11958== 
==11958== Use of uninitialised value of size 8
==11958==    at 0x400513: main (in /home/cxq/test)
==11958==  Uninitialised value was created by a stack allocation
==11958==    at 0x400507: main (in /home/cxq/test)
==11958== 
==11958== Invalid read of size 1
==11958==    at 0x400513: main (in /home/cxq/test)
==11958==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==11958== 
==11958== 
==11958== Process terminating with default action of signal 11 (SIGSEGV)
==11958==  Access not within mapped region at address 0x0
==11958==    at 0x400513: main (in /home/cxq/test)
==11958==  If you believe this happened as a result of a stack
==11958==  overflow in your program's main thread (unlikely but
==11958==  possible), you can try to increase the size of the
==11958==  main thread stack using the --main-stacksize= flag.
==11958==  The main thread stack size used in this run was 8388608.
==11958== 
==11958== HEAP SUMMARY:
==11958==     in use at exit: 0 bytes in 0 blocks
==11958==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==11958== 
==11958== All heap blocks were freed -- no leaks are possible
==11958== 
==11958== For lists of detected and suppressed errors, rerun with: -s
==11958== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault

输出结果显示了程序中存在的两个主要问题:一个是“使用未初始化的值”,另一个是“无效的内存读取”

Valgrind 报告指出程序在 main 函数的两个位置(内存地址 0x400513 和 0x400507)使用了未初始化的值。这通常意味着变量在使用前没有被赋予一个明确的初始值,它可能包含任意的垃圾数据

Valgrind 还报告了一个无效的内存读取错误,这表明程序试图读取一个它没有权限访问的内存地址(地址 0x0)。这通常是因为指针错误或访问已经释放的内存导致的。

检测内存泄漏

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

int main(void)
{
    char *p = malloc(1);
    *p = 'a';
    char c = *p;
    printf("\n [%c]\n", c);

    return 0;
}

在linux系统下 ,将test.c文件变成进程

gcc  test.c -o test

valgrind 命令:
valgrind --leak-check=full --tool=memcheck ./test

[cxq@iZwz9fjj2ssnshikw14avaZ ~]$ valgrind --leak-check=full --tool=memcheck  ./test
==13913== Memcheck, a memory error detector
==13913== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13913== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==13913== Command: ./test
==13913== 

 [a]
==13913== 
==13913== HEAP SUMMARY:
==13913==     in use at exit: 1 bytes in 1 blocks
==13913==   total heap usage: 1 allocs, 0 frees, 1 bytes allocated
==13913== 
==13913== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==13913==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==13913==    by 0x400558: main (in /home/cxq/test)
==13913== 
==13913== LEAK SUMMARY:
==13913==    definitely lost: 1 bytes in 1 blocks
==13913==    indirectly lost: 0 bytes in 0 blocks
==13913==      possibly lost: 0 bytes in 0 blocks
==13913==    still reachable: 0 bytes in 0 blocks
==13913==         suppressed: 0 bytes in 0 blocks
==13913== 
==13913== For lists of detected and suppressed errors, rerun with: -s
==13913== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

在这里插入图片描述

在内存被释放后进行读/写

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

int main(void) {
    char *p = malloc(1);
    *p = 'a';
    char c = *p;

    printf("\n [%c]\n", c);

    free(p);
    c = *p;

    return 0;
}

在linux系统下 ,将test.c文件变成进程

gcc  test.c -o test

valgrind 命令:
valgrind --tool=memcheck ./test

[cxq@iZwz9fjj2ssnshikw14avaZ ~]$ valgrind --tool=memcheck  ./test
==14269== Memcheck, a memory error detector
==14269== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==14269== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==14269== Command: ./test
==14269== 

 [a]
==14269== Invalid read of size 1
==14269==    at 0x4005E3: main (in /home/cxq/test)
==14269==  Address 0x5205040 is 0 bytes inside a block of size 1 free'd
==14269==    at 0x4C2B06D: free (vg_replace_malloc.c:540)
==14269==    by 0x4005DE: main (in /home/cxq/test)
==14269==  Block was alloc'd at
==14269==    at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==14269==    by 0x4005A8: main (in /home/cxq/test)
==14269== 
==14269== 
==14269== HEAP SUMMARY:
==14269==     in use at exit: 0 bytes in 0 blocks
==14269==   total heap usage: 1 allocs, 1 frees, 1 bytes allocated
==14269== 
==14269== All heap blocks were freed -- no leaks are possible
==14269== 
==14269== For lists of detected and suppressed errors, rerun with: -s
==14269== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鄃鳕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值