使用valgrind检查内存越界

简介: 使用valgrind检查内存越界

下载

http://valgrind.org/downloads/current.html


解压

编译

./configure
make
sudo make install

测试

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
    //char  array[32] = {0};
    char* buffer = NULL;
    buffer = (char*)malloc(32);
    //崩溃
    buffer[56] = 0;
    free(buffer);
    return 0;
}

编译运行


gcc -g test.c
valgrind --tool=memcheck --leak-check=full ./a.out

显示如下:


==13794== Memcheck, a memory error detector
==13794== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13794== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==13794== Command: ./a.out
==13794== 
==13794== Invalid write of size 1
==13794==    at 0x400593: main (test.c:11)
==13794==  Address 0x5205078 is 24 bytes after a block of size 32 in arena "client"
==13794== 
==13794== 
==13794== HEAP SUMMARY:
==13794==     in use at exit: 0 bytes in 0 blocks
==13794==   total heap usage: 1 allocs, 1 frees, 32 bytes allocated
==13794== 
==13794== All heap blocks were freed -- no leaks are possible
==13794== 
==13794== For counts of detected and suppressed errors, rerun with: -v
==13794== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)


注意其中:


==13794== Invalid write of size 1
==13794==    at 0x400593: main (test.c:11)

实用性探讨

需要说明的是,对于类似char  array[32] = {0};,提示上就要差一些


==13772== Invalid read of size 1
==13772==    at 0x4E5B800: (below main) (libc-start.c:285)
==13772==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

具体情况,以后通过试用再做补充.

目录
相关文章
|
2月前
|
Linux
linux 常用内存检查命令
linux 常用内存检查命令
43 0
|
2月前
|
Rust 安全 编译器
Rust中的生命周期与借用检查器:内存安全的守护神
本文深入探讨了Rust编程语言中生命周期与借用检查器的概念及其工作原理。Rust通过这些机制,在编译时确保了内存安全,避免了数据竞争和悬挂指针等常见问题。我们将详细解释生命周期如何管理数据的存活期,以及借用检查器如何确保数据的独占或共享访问,从而在不牺牲性能的前提下,为开发者提供了强大的内存安全保障。
|
6月前
|
缓存 Linux
百度搜索:蓝易云【检查 Linux 系统内存使用量是否耗尽?这5个命令堪称绝了!详解!】
通过使用以上这五个命令,你可以全面了解Linux系统的内存使用情况,从而判断是否存在内存耗尽的问题。这些命令提供了不同层次和角度的内存信息,帮助你定位和解决与内存相关的性能问题。
91 0
|
存储 缓存 监控
如何检查 Linux 内存使用量是否耗尽?这5个命令堪称绝了!
如何检查 Linux 内存使用量是否耗尽?这5个命令堪称绝了!
169 0
|
Ubuntu
UBUNTU设置环境变量MALLOC_CHECK_=1检查内存
UBUNTU设置环境变量MALLOC_CHECK_=1检查内存
113 0
|
Linux
LINUX检查一个进程内存增长的脚本
LINUX检查一个进程内存增长的脚本
100 0
|
存储 Web App开发 JSON
检查自己的代码是否存在内存泄露
造成内存泄露的根本原因就是我们写的代码中存在某些对象长期占用内存,得不到释放,且这个对象占用的内存会逐步增加,导致 v8 无法回收,从而造成的服务的异常和不稳定,甚至是服务的中断和崩溃。
249 0
检查自己的代码是否存在内存泄露
|
Windows
svchost.exe占用内存过高--(windows update检查更新时一直处于正在检查)
学习.exe占用内存过高--(windows update检查更新时一直处于正在检查)。
190 0
|
缓存 监控 网络协议
Linux:常用性能检查命令(内存、CPU 、网络、磁盘、Java应用)
Linux:常用性能检查命令(内存、CPU 、网络、磁盘、Java应用)
843 0
Linux:常用性能检查命令(内存、CPU 、网络、磁盘、Java应用)
重载下标操作符,检查内存越界
重载下标操作符,检查内存越界
100 0

热门文章

最新文章