valgrind调试c/c++内存问题:非法地址访问_内存泄漏_越界访问

1.valgrind命令

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

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

实例

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");
    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 1

申请3次内存,少释放一次,主动触发 内存泄漏
2981 HEAP SUMMARY:
2981 in use at exit: 20 bytes in 1 blocks
2981 total heap usage: 4 allocs, 3 frees, 1,060 bytes allocated -->4次申请,3次释放,少一次free
2981
2981 20 bytes in 1 blocks are definitely lost in loss record 1 of 1
2981 at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
2981 by 0x1092A7: manual_leak_mem (52_valgrind_内存泄漏提示.c:23) -->内存泄漏提示行
2981 by 0x109318: main (52_valgrind_内存泄漏提示.c:37)
2981
2981 LEAK SUMMARY:
2981 definitely lost: 20 bytes in 1 blocks
2981 indirectly lost: 0 bytes in 0 blocks
2981 possibly lost: 0 bytes in 0 blocks
2981 still reachable: 0 bytes in 0 blocks
2981 suppressed: 0 bytes in 0 blocks
2981
2981 For lists of detected and suppressed errors, rerun with: -s
2981 ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

总结:
lost in loss record 丢失记录 --> 内存泄漏
在这里插入图片描述

3.非法地址访问

3.1非法地址读

// 2.非法地址访问
void invalid_address_access(void)
{
    // uint8_t *p = 0x12345678; # 非法地址
    uint8_t *p = NULL;
    printf("val = %d\n", *p);
}

valgrind --leak-check=full ./51_mem_valgrind调试_内存泄漏_非法地址访问_越界.out 2

13358 Command: ./51_mem_valgrind__.out 3
13358
13358 Invalid read of size 1 无效读地址
13358 at 0x109234: invalid_address_access (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:18) 提示错误行
13358 by 0x109312: main (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:47)
13358 Address 0x0 is not stack’d, malloc’d or (recently) free’d
13358
13358
13358 Process terminating with default action of signal 11 (SIGSEGV) SIGSEGV非法地址访问
13358 Access not within mapped region at address 0x0
13358 at 0x109234: invalid_address_access (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:18)
13358 by 0x109312: main (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:47)
13358 If you believe this happened as a result of a stack
13358 overflow in your program’s main thread (unlikely but
13358 possible), you can try to increase the size of the
13358 main thread stack using the --main-stacksize= flag.
13358 The main thread stack size used in this run was 8388608.
13358
13358 HEAP SUMMARY:
13358 in use at exit: 0 bytes in 0 blocks
13358 total heap usage: 0 allocs, 0 frees, 0 bytes allocated
13358
13358 All heap blocks were freed – no leaks are possible
13358
13358 For lists of detected and suppressed errors, rerun with: -s
13358 ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

3.1非法地址写

// 2.非法地址访写
void invalid_address_write(void)
{
    // uint8_t *p = 0x12345678; # 非法地址
    uint8_t *p = NULL;
    *p = 0x12;
}

valgrind --leak-check=full ./51_mem_valgrind调试_内存泄漏_非法地址访问_越界.out 3

17929 Invalid write of size 1 非法地址写
17929 at 0x109267: invalid_address_write (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:26)
17929 by 0x10933F: main (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:56)
17929 Address 0x0 is not stack’d, malloc’d or (recently) free’d
17929
17929
17929 Process terminating with default action of signal 11 (SIGSEGV) 段错误
17929 Access not within mapped region at address 0x0
17929 at 0x109267: invalid_address_write (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:26) 提示行
17929 by 0x10933F: main (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:56)
17929 If you believe this happened as a result of a stack
17929 overflow in your program’s main thread (unlikely but
17929 possible), you can try to increase the size of the
17929 main thread stack using the --main-stacksize= flag.
17929 The main thread stack size used in this run was 8388608.
17929
17929 HEAP SUMMARY:
17929 in use at exit: 0 bytes in 0 blocks
17929 total heap usage: 0 allocs, 0 frees, 0 bytes allocated
17929
17929 All heap blocks were freed – no leaks are possible
17929
17929 For lists of detected and suppressed errors, rerun with: -s
17929 ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault 段错误

4.内存越界

1.栈越界读

// 4.越界 读写
void corss_border_read_write(void)
{
    uint32_t a = 0x10;
    uint32_t *p = &a + 0x8;

    // 越界读 -- 程序不崩溃,可能导致逻辑错误
    printf("cross border address read %x\n", *p);

    // 越界写 -- 程序错误
    *p = 0x12345678;
    //printf("cross border address write %x\n", *p);
}

运行无错误提示
valgrind --leak-check=full --show-leak-kinds=all ./51_mem_valgrind调试_内存泄漏_非法地址访问_越界.out 4

2.栈越界写

// 4.越界 读写
void corss_border_read_write(void)
{
    uint32_t a = 0x10;
    uint32_t *p = &a + 0x8;

    // 越界读 -- 程序不崩溃,可能导致逻辑错误
    // printf("cross border address read %x\n", *p);

    // 越界写 -- 程序错误
    *p = 0x12345678;
    printf("cross border address write %x\n", *p);
}

valgrind --leak-check=full ./51_mem_valgrind调试_内存泄漏_非法地址访问_越界.out 4

cross border address read 0
cross border address write 12345678
23861 Jump to the invalid address stated on the next line 非法地址
23861 at 0x123456780010935C: ???
23861 by 0x48A1D8F: (below main) (libc_start_call_main.h:58)
23861 Address 0x123456780010935c is not stack’d, malloc’d or (recently) free’d
23861
23861
23861 Process terminating with default action of signal 11 (SIGSEGV) 触发段错误
23861 Bad permissions for mapped region at address 0x123456780010935C
23861 at 0x123456780010935C: ???
23861 by 0x48A1D8F: (below main) (libc_start_call_main.h:58)
23861
23861 HEAP SUMMARY:
23861 in use at exit: 1,024 bytes in 1 blocks
23861 total heap usage: 1 allocs, 0 frees, 1,024 bytes allocated
23861
23861 LEAK SUMMARY:
23861 definitely lost: 0 bytes in 0 blocks
23861 indirectly lost: 0 bytes in 0 blocks
23861 possibly lost: 0 bytes in 0 blocks
23861 still reachable: 1,024 bytes in 1 blocks
23861 suppressed: 0 bytes in 0 blocks
23861 Reachable blocks (those to which a pointer was found) are not shown.
23861 To see them, rerun with: --leak-check=full --show-leak-kinds=all
23861
23861 For lists of detected and suppressed errors, rerun with: -s
23861 ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault

更详细的显示
valgrind --leak-check=full --show-leak-kinds=all ./51_mem_valgrind调试_内存泄漏_非法地址访问_越界.out 4

cross border address write 12345678
25992 Jump to the invalid address stated on the next line
25992 at 0x1234567800109340: ???
25992 by 0x48A1D8F: (below main) (libc_start_call_main.h:58)
25992 Address 0x1234567800109340 is not stack’d, malloc’d or (recently) free’d
25992
25992
25992 Process terminating with default action of signal 11 (SIGSEGV)
25992 Bad permissions for mapped region at address 0x1234567800109340
25992 at 0x1234567800109340: ???
25992 by 0x48A1D8F: (below main) (libc_start_call_main.h:58)
25992
25992 HEAP SUMMARY:
25992 in use at exit: 1,024 bytes in 1 blocks
25992 total heap usage: 1 allocs, 0 frees, 1,024 bytes allocated
25992
25992 1,024 bytes in 1 blocks are still reachable in loss record 1 of 1 ==到达 丢失的记录 ==
25992 at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
25992 by 0x48D879E: printf (printf.c:33)
25992 by 0x1092C0: corss_border_read_write (51_mem_valgrind调试_内存泄漏_非法地址访问_越界.c:40)
25992 by 0x123456780010933F: ???
25992 by 0x48A1D8F: (below main) (libc_start_call_main.h:58)
25992
25992 LEAK SUMMARY:
25992 definitely lost: 0 bytes in 0 blocks
25992 indirectly lost: 0 bytes in 0 blocks
25992 possibly lost: 0 bytes in 0 blocks
25992 still reachable: 1,024 bytes in 1 blocks
25992 suppressed: 0 bytes in 0 blocks
25992
25992 For lists of detected and suppressed errors, rerun with: -s
25992 ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/748715.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

vue3-cropperjs图片裁剪工具-用户上传图片截取-(含预览视频)

效果图 上传图片弹窗预览 对于这个上传图片样式可以参考 官方原代码 官网传送入口 Upload 上传 | Element Plus (element-plus.org) <template><el-uploadclass"upload-demo"dragaction"https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6…

吴恩达LangChain教程:Embedding与文档解析

当前有很多应用想要实现根据文档或者文本内容实现用户问答&#xff0c;或者实现多轮会话能力&#xff0c;这时候就会使用到Embedding的能力。 01 | 使用类介绍 想要依据Embedding实现文本检索&#xff0c;需要引入如下的依赖。 其中&#xff0c;RetrievalQA的作用是对一些文档…

一天跌20%,多只可转债“腰斩”,近百只跌破面值,“退可守”的香饽饽为何破防?

专业人士指出&#xff0c;近期部分可转债大跌原因主要有两点&#xff1a;一方面&#xff0c;转债市场与权益市场联动性强。另一方面&#xff0c;近期公布的宏观经济数据稳中趋缓&#xff0c;“供强需弱”特征依然明显&#xff0c;证监会主席吴清发言及“科创板八条”新规延续了…

Python 基础 (标准库):heapq (堆)

1. 官方文档 heapq --- 堆队列算法 — Python 3.12.4 文档 2. 相关概念 堆 heap 是一种具体的数据结构&#xff08;concrete data structures&#xff09;&#xff1b;优先级队列 priority queue 是一种抽象的数据结构&#xff08;abstract data structures&#xff09;&…

秘籍来啦!手机找回删除的文件,掌握这3种方法

你是否曾经不小心删除了手机上的重要文件&#xff0c;如照片、视频、文档等&#xff0c;然后束手无策&#xff0c;不知道该如何恢复&#xff1f;这时候&#xff0c;找回删除的文件就显得尤为重要。别担心&#xff0c;本文将为大家揭秘3种方法&#xff0c;让你轻松找回那些被删除…

Redis Stream Redisson Stream

目录 一、Redis Stream1.1 场景1&#xff1a;多个客户端可以同时接收到消息1.1.1 XADD - 向stream添加Entry&#xff08;发消息 &#xff09;1.1.2 XREAD - 从stream中读取Entry&#xff08;收消息&#xff09;1.1.3 XRANGE - 从stream指定区间读取Entry&#xff08;收消息&…

徐徐拉开的帷幕:拜登与特朗普的辩论大戏 日元跌破160大关!创1986年以来最低纪录

北京时间6月27日&#xff08;本周五&#xff09;上午9:00&#xff0c;拜登和特朗普将参加2024年总统候选人电视辩论。作为参考&#xff0c;2016年大选辩论期间&#xff0c;美元汇率对辩论结果的反应相对温和&#xff0c;希拉里胜选预期增强在一定程度上支撑了美元。 时间逐渐临…

AI产品打造全攻略:看我是如何预测用户流失,搞定AI产品全流程的

前言 对于任何互联网公司而言&#xff0c;用户流失无疑是一个不容忽视的问题。在本文中&#xff0c;我将通过一个真实的预测用户流失的项目案例&#xff0c;带领大家深入了解AI产品从筹备到上线的整个流程。这个过程将展现AI产品经理的工作全貌&#xff0c;包括各个环节的角色…

钉钉在MAKE 2024大会上宣布开放AI生态;NBC将用AI主播播报巴黎奥运会内容

&#x1f680; 钉钉在MAKE 2024大会上宣布开放AI生态 摘要&#xff1a;钉钉总裁叶军在MAKE 2024生态大会上宣布&#xff0c;钉钉将对所有大模型厂商开放&#xff0c;构建“国内最开放AI生态”。目前已有六家大模型厂商接入钉钉&#xff0c;用户可直接使用七家大模型产品。未来…

下拉选择输入框(基于elment-ui)

最近在需求中&#xff0c;需要有一个下拉选择功能&#xff0c;又得可以输入&#xff0c;在 element-ui 官网找了&#xff0c;发现没有适合的&#xff0c;然后在修炼 cv 大法的我&#xff0c;也在网上看了一下&#xff0c;但是也都感觉不合适&#xff0c;所以就自己写了一个&…

R语言数据分析案例37-旅游景点聚类分析

一、研究背景 近年来&#xff0c;随着旅游业的迅猛发展&#xff0c;旅游景点的竞争日益激烈。如何在众多景点中脱颖而出&#xff0c;吸引更多游客&#xff0c;成为各大景点管理者关注的焦点。通过对旅游景点进行深入的数据分析&#xff0c;可以帮助管理者更好地了解景点的优势…

C#1.0-11.0所有历史版本主要特性总结

文章目录 前言名词解释主要版本一览表各版本主要特性一句话总结 C# 1.0 (Visual Studio 2002, .Net Framework 1.0)C# 2.0 (Visual Studio 2005, .Net Framework 2.0)C# 3.0 (Visual Studio 2008, .Net Framework 3.0)C# 4.0 (Visual Studio 2010, .Net Framework 4)C# 5.0 (V…

赏金猎人src挖掘入门

文章目录 1. 什么是漏洞2. OWASP Top 103. 利用的漏洞来源4. SRC安全应急响应中心5. Burpsuite简介6. 浏览器代理插件6.1 firefox浏览器代理插件6.2 edge浏览器代理插件3.chrome浏览器代理插件&#xff08;需要科学上网&#xff09; 1. 什么是漏洞 漏洞是指一个系统存在的弱点或…

2024广东省职业技能大赛云计算赛项实战——构建CICD

构建CI/CD 前言 题目如下&#xff1a; 构建CI/CD 编写流水线脚本.gitlab-ci.yml触发自动构建&#xff0c;具体要求如下&#xff1a; &#xff08;1&#xff09;基于镜像maven:3.6-jdk-8构建项目的drone分支&#xff1b; &#xff08;2&#xff09;构建镜像的名称&#xff1a…

C# VTK 自定义封装 vtkwPipeline 多边形管道建模

vtkwPipeline 简介 public vtkwPipeline(vtkLineSource lineSource, double outR, double inR, int sides) vtkwPipeline 是我自定义封装的C# 类 用于对管道壁建模&#xff0c;有内半径&#xff0c;外半径设置&#xff0c; 以及多边形边数设置。 参数 1. vtkLineSource li…

EI CCIE学习笔记-SDAccess之一:SDAccess解决方案

Chapter 1 SD-Access Solution Proposal 1.1 概念引入 SDN三要素&#xff1a;集中控制、转控分离、可编程 DNA DNA:Digital Network Architecture数字网络架构 思科提出的跨园区&#xff0c;分支机构&#xff0c;WAN和扩展企业的企业网络架构它提供了一种开放&#xff0c;可扩…

win10 C:\Users\Administrator

win10 C:\Users\Administrator C:\Users\Administrator\Documents\ C:\Users\Administrator\Pictures C:\Users\Administrator\Favorites C:\Users\Administrator\Links C:\Users\Administrator\Videos

Shopee API接口——获取商家店铺商品列表

一、引言 在跨境电商领域&#xff0c;Shopee作为东南亚地区领先的电商平台&#xff0c;为众多商家提供了广阔的市场和丰富的销售机会。本文将详细介绍如何通过Shopee API获取商家店铺商品列表&#xff0c;并探讨其应用场景。 二、核心功能介绍 Shopee API获取商家店铺商品列…

数据结构(Java):ArrayList的应用

1、引言 上一篇博客&#xff0c;已经为大家讲解了集合类ArrayList。 这篇博客&#xff0c;就来帮助大家学会使用ArrayList。 2、题1&#xff1a; 删除字符&#xff08;热身题&#xff09; 题目&#xff1a;给出str1和str2两个字符串&#xff0c;删除str1中出现的所有的str2…

【线代基础】张宇30讲+300题错题整理

第一章 行列式 1. 2. 第二章 矩阵 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 第三章 向量 1. 2. 3. 第四章 线性方程组 1. 2. 3. 4. 5. 6. 7. 8. 9. 第五章 特征值与特征向量 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 第六章 二次型 1. 2. 3. 4. 5. 终于结束了线性…