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


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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

科技助力行政执法:4G无线网络技术在管理指挥中心的应用

随着科技的飞速发展&#xff0c;4G无线网络技术已经越来越成熟&#xff0c;为行政执法管理带来了前所未有的便利与效率。特别是在管理指挥中心&#xff0c;通过实时观看高清现场画面&#xff0c;执法人员可以随时进行调度指挥&#xff0c;掌握行政执法队伍的全过程&#xff0c;…

Bev系列算法总结

文章目录 1. LSS-Based1.1 BevDet1.2 BevDepth1.3 BevStereo1.4 SoloFusion1.4 VideoBev1.5 总结2. Bev IPM Based(3D to 2D)2.1 Bevformer v12.1 Bevformer v22. sparse query2.1 petr v12.2 petr v22.3 stream petr2.4 DETR 3d2.5 sparse4Dsparse4D v11. LSS-Based 1.1 Be…

llamafactory-llama3微调中文数据集

一、定义 https://github.com/SmartFlowAI/Llama3-Tutorial/tree/main 基准模型测试opencompass 离线测评数据准备微调训练合并测试人工审核对比 二、实现 基准模型测试 基准模型 llama3-8b https://zhuanlan.zhihu.com/p/694818596? https://github.com/SmartFlowAI/Llam…

品牌窜货治理:维护市场秩序与品牌健康的关键

品牌在各个渠道通常都会设定相应的销售规则&#xff0c;其中常见的便是区域保护制度&#xff0c;比如 A 地区的货物只能在 A 地区销售&#xff0c;各区域的产品价格和销售策略均有所不同&#xff0c;因此 A 地区的货物不能流向 B 地区&#xff0c;否则就被称为窜货。 窜货现象不…

劳易测应用案例:橡胶密炼生产线安全改造项目(下)

橡胶密炼是汽车轮胎制造流程中的核心环节&#xff0c;主要负责将橡胶与多种添加剂混合&#xff0c;确保均匀分散&#xff0c;以制备合格的橡胶材料。橡胶密炼生产线由多个关键设备组成&#xff0c;包括切胶机、导切机、称重和输送系统、密炼机、开炼机以及胶片冷却机等&#xf…

匠心铸就服务品质,全视通技术服务获盘锦市中医医院高度认可

一声表扬&#xff0c;万分肯定 寥寥数语&#xff0c;情意深重 承载着荣誉 道出了心声 传达了谢意 倾注了期盼 字里行间的内容 是对全视通技术服务的高度认可 记录了全视通与盘锦市中医医院之间的双向奔赴 盘锦市中医医院表扬信是对全视通技术服务团队工作的高度认可&am…

Xilinx FPGA:vivado实现串口的接收端

补充一些串口里用到的数值的相关知识点 接收端串口时序图&#xff1a; 程序设计&#xff1a; timescale 1ns / 1ps /串口接收端 串行转并行 module uart_rx(input sys_clk ,input rst_n ,input rx_data , //输入…

【C++】相机标定源码笔记-通用工具函数类

提供了一系列工具函数及处理方法&#xff0c;主要用于图像处理、点云处理和文件操作等领域。以下是对关键函数的简要解析&#xff1a; 点云处理与平面拟合 包含两个重载函数&#xff0c;一个接受Eigen矩阵类型的点集&#xff0c;另一个接受pcl::PointCloud<pcl::PointXYZ>…

【Java中导出Excel导出多个sheet页】

Java中导出Excel导出多个sheet页 序言如何处理多个sheet页的导出期间遇到了一个sheet页相关的问题&#xff0c;以及解决办法多sheet页导出遇到&#xff0c;第二个sheet页的标题名称会把第一个的覆盖的问题 结语 序言 在日常工作中经常有导出数据文件的需求&#xff0c;避免不了…

工具篇:鸿蒙DevEco Studio5.0版本下载及安装

1、下载中心地址 下载中心 | 华为开发者联盟-HarmonyOS开发者官网&#xff0c;共建鸿蒙生态 2、安装 DevEco Studio支持Windows和macOS系统&#xff0c;下面将针对两种操作系统的软件安装方式分别进行介绍。 Windows环境 运行环境要求 为保证DevEco Studio正常运行&#…

电机驱动知识点总结

一、直流电机入门基础知识 1.直流电机原理 下面是分析直流电机的物理模型图。其中&#xff0c;固定部分有磁铁&#xff0c;这里称作主磁极&#xff1b;固定部分还有电刷。转动部分有环形铁心和绕在环形铁心上的绕组。(其中 2 个小圆圈是为了方便表示该位置上的导体电势或电流…

GaussDB关键技术原理:高性能(二)

GaussDB关键技术原理&#xff1a;高性能&#xff08;一&#xff09;从数据库性能优化系统概述对GaussDB的高性能技术进行了解读&#xff0c;本篇将从查询处理综述方面继续分享GaussDB的高性能技术的精彩内容。 2 查询处理综述 内容概要&#xff1a;本章节介绍查询端到端处理的…

基于STM32F103最小系统板和DL-LN33 2.4G通信 ZigBee无线串口自组网采集温湿度

文章目录 前言一、组网概述二、产品特性三、电气特性四、引脚配置五、UART通信协议5.1 UART参数5.2 包分割5.3 端口5.4 举例通信5.4.1 一个节点给另一个节点发送数据5.4.2 一个节点给另一个节点的内部端口发送数据5.4.3 一个节点给自己的内部端口发送数据5.4.4 不推荐的数据传输…

气膜体育馆的安装流程—轻空间

随着人们对健康生活和高品质运动环境的追求&#xff0c;气膜体育馆因其独特的优点而逐渐受到青睐。轻空间将详细介绍气膜体育馆的安装流程&#xff0c;从实地勘测到检测&#xff0c;再到最终的清理现场&#xff0c;每一步都至关重要&#xff0c;确保体育馆的安全性和功能性。 一…

内网穿透与异地组网强强联合,这款工具屌爆了!!!

在数字化飞速发展的今天&#xff0c;远程访问的需求日益增长&#xff0c;网络已成为我们生活和工作中不可或缺的一部分。然而&#xff0c;远程网络连接的稳定性和安全性往往是我们关注的焦点。节点小宝作为一款创新型的远程管理工具&#xff0c;凭借其使用简单&#xff0c;高速…

RabbitMQ WEB管理端介绍

页面功能概览 Overview(概述)Connections(连接)Channels(通道)Exchanges(交换器)Queues(队列)Admin(用户管理)。 1. Overview(概述) 主要分为三部分 1.1 Queued messages&#xff08;所有队列的消息情况&#xff09; Ready&#xff1a;待消费的消息总数Unacked&#xff1a;待应…

实时美颜技术解析:视频美颜SDK如何改变直播行业

实时美颜技术的出现&#xff0c;尤其是视频美颜SDK的应用&#xff0c;正逐渐改变着直播行业的生态。 一、实时美颜技术的原理 实时美颜技术利用人工智能和图像处理算法&#xff0c;对视频中的人物面部进行优化和修饰。该技术通常包含以下几个步骤&#xff1a; 1.人脸检测和识…

OpenGL->OpenGL环境搭建和配置

OpenGL概述 OpenGL&#xff1a;用于渲染2D、3D矢量图形的APIGLEW&#xff1a;是一个跨平台的C/C库&#xff0c;能够使用更高版本的OpenGL扩展特性GLFW&#xff1a;创建和管理OpenGL上下文&#xff0c;管理渲染窗口&#xff0c;处理输入事件&#xff0c;更轻量GLUT&#xff1a;…

视频格式转换方法:如何使用视频转换器软件转换视频

众所周知&#xff0c;目前存在许多不同的视频和音频格式。但我们的媒体播放器、移动设备、PC 程序等仅兼容少数特定格式。例如&#xff0c;如果不先将其转换为 MP4、MOV 或 M4V 文件&#xff0c;AVI、WMV 或 MKV 文件就无法在 iPhone 上播放。 视频转换器允许您将一种视频格式…

解决微信小程序使用textarea输入框 type=“textarea“ 文本输入限制问题

出现的问题 type"textarea" 这个限制 微信小程序使用textarea , 输入字数大于140 时就输入不进去了 加入这个就解决了 maxlength"-1" <u-inputv-model"queryParams.orderIdTxt"border"true":focus"true":auto-height&q…