admin管理员组

文章数量:1130349

1.valgrind功能简介

程序崩溃的 coredump文件, 通过gdb 能定位非法地址访问位置问题,
但对于内存泄漏, 内存越界访问, 无有效的提示.
valgrind 功能强大, 可以调试定位常见内存问题:

  • 非法地址访问
  • 内存泄漏
  • 内存越界读写

命令
调试内存问题: valgrind --leak-check=full
更新详细的显示: valgrind --leak-check=full --show-leak-kinds=all

2.valgrind提示信息汇总

  • 内存泄漏 lost in loss record 丢失记录 , 内存泄漏实例[[#2.内存泄漏–不完全释放内存|实例链接]]
  • 段错误 Process terminating with default action of signal 11 (SIGSEGV)
    • 非法地址读 Invalid read of size 1 非法地址读实例 [[#3.1非法地址读实例]]
    • 非法地址写 Invalid write of size 1 非法地址写实例[[#3.1非法地址写]]
  • 越界访问
    • 栈越界读 --无异常
    • 栈越界写 实例[[#2.栈越界写]]
      • Jump to the invalid address stated on the next line
      • reachable in loss record
      • still reachable:

测试代码链接: gitee-51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c

3.实例

1.无内存泄漏–完全释放内存

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

void full_calloc_free(void)
{
   
   
    printf("申请3次内存,并释放\n");
    int * p_data[3];
    p_data[0] = malloc(sizeof(int));
    p_data[1] = malloc(sizeof(int)*3);
    p_data[2] = malloc(sizeof(int)*5);

    free(p_data[0]);
    free(p_data[1]);
    free(p_data[2]);
}

valgrind --leak-check=full ./52_valgrind_内存泄漏提示.out

2921 Command: ./52_valgrind___.out
申请3次内存,并释放
2921
2921 HEAP SUMMARY:
2921 in use at exit: 0 bytes in 0 blocks
2921 total heap usage: 4 allocs, 4 frees, 1,060 bytes allocated -->申请4次,释放4次
2921
2921 All heap blocks were freed – no leaks are possible -->无内存泄漏可能性
2921
2921 For lists of detected and suppressed errors, rerun with: -s
2921 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

总结:
无内存泄漏的程序, 运行结束, 应该提示无内存泄漏

2.内存泄漏–不完全释放内存

void manual_leak_mem(void)
{
   
   
    printf("申请3次内存,少释放一次,主动触发 内存泄漏\n"

1.valgrind功能简介

程序崩溃的 coredump文件, 通过gdb 能定位非法地址访问位置问题,
但对于内存泄漏, 内存越界访问, 无有效的提示.
valgrind 功能强大, 可以调试定位常见内存问题:

  • 非法地址访问
  • 内存泄漏
  • 内存越界读写

命令
调试内存问题: valgrind --leak-check=full
更新详细的显示: valgrind --leak-check=full --show-leak-kinds=all

2.valgrind提示信息汇总

  • 内存泄漏 lost in loss record 丢失记录 , 内存泄漏实例[[#2.内存泄漏–不完全释放内存|实例链接]]
  • 段错误 Process terminating with default action of signal 11 (SIGSEGV)
    • 非法地址读 Invalid read of size 1 非法地址读实例 [[#3.1非法地址读实例]]
    • 非法地址写 Invalid write of size 1 非法地址写实例[[#3.1非法地址写]]
  • 越界访问
    • 栈越界读 --无异常
    • 栈越界写 实例[[#2.栈越界写]]
      • Jump to the invalid address stated on the next line
      • reachable in loss record
      • still reachable:

测试代码链接: gitee-51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c

3.实例

1.无内存泄漏–完全释放内存

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

void full_calloc_free(void)
{
   
   
    printf("申请3次内存,并释放\n");
    int * p_data[3];
    p_data[0] = malloc(sizeof(int));
    p_data[1] = malloc(sizeof(int)*3);
    p_data[2] = malloc(sizeof(int)*5);

    free(p_data[0]);
    free(p_data[1]);
    free(p_data[2]);
}

valgrind --leak-check=full ./52_valgrind_内存泄漏提示.out

2921 Command: ./52_valgrind___.out
申请3次内存,并释放
2921
2921 HEAP SUMMARY:
2921 in use at exit: 0 bytes in 0 blocks
2921 total heap usage: 4 allocs, 4 frees, 1,060 bytes allocated -->申请4次,释放4次
2921
2921 All heap blocks were freed – no leaks are possible -->无内存泄漏可能性
2921
2921 For lists of detected and suppressed errors, rerun with: -s
2921 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

总结:
无内存泄漏的程序, 运行结束, 应该提示无内存泄漏

2.内存泄漏–不完全释放内存

void manual_leak_mem(void)
{
   
   
    printf("申请3次内存,少释放一次,主动触发 内存泄漏\n"

本文标签: 内存地址Valgrind