#include <stdlib.h>
#include <malloc.h>
#include <string.h>
void test()
{
int *ptr = malloc(sizeof(int)*10);
ptr[10] = 7; // 内存越界
memcpy(ptr +1, ptr, 5); // 踩内存
free(ptr);
free(ptr);// 重复释放
int *p1;
*p1 = 1; // 非法指针
}
int main(void)
{
test();
return 0;
}
将程序编译生成可执行文件后执行: valgrind --leak-check=full ./程序名
输出结果如下:
==4832== Memcheck, a memory error detector
==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==4832== Command: ./tmp
==4832==
==4832== Invalid write of size 4 // 内存越界
==4832== at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd
==4832== at 0x4026864: malloc (vg_replace_malloc.c:236)
==4832== by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==
==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩内存
==4832== at 0x4027BD6: memcpy (mc_replace_strmem.c:635)
==4832== by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==
==4832== Invalid free() / delete / delete[] // 重复释放
==4832== at 0x4025BF0: free (vg_replace_malloc.c:366)
==4832== by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== Address 0x41a6028 is 0 bytes inside a block of size 40 free'd
==4832== at 0x4025BF0: free (vg_replace_malloc.c:366)
==4832== by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==
==4832== Use of uninitialised value of size 4 // 非法指针
==4832== at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==
==4832==
==4832== Process terminating with default action of signal 11 (SIGSEGV) //由于非法指针赋值导致的程序崩溃
==4832== Bad permissions for mapped region at address 0x419FFF4
==4832== at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==
==4832== HEAP SUMMARY:
==4832== in use at exit: 0 bytes in 0 blocks
==4832== total heap usage: 1 allocs, 2 frees, 40 bytes allocated
==4832==
==4832== All heap blocks were freed -- no leaks are possible
==4832==
==4832== For counts of detected and suppressed errors, rerun with: -v
==4832== Use --track-origins=yes to see where uninitialised values come from
==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)
Segmentation fault
从valgrind的检测输出结果看,这几个错误都找了出来。
lj_70596
- 粉丝: 101
- 资源: 3934
最新资源
- 两相步进电机FOC矢量控制Simulink仿真模型 1.采用针对两相步进电机的SVPWM控制算法,实现FOC矢量控制,DQ轴解耦控制~ 2.转速电流双闭环控制,电流环采用PI控制,转速环分别采用PI和
- VMware虚拟机USB驱动
- Halcon手眼标定简介(1)
- (175128050)c&c++课程设计-图书管理系统
- 视频美学多任务学习中PyTorch的多回归实现-含代码及解释
- 基于ssh员工管理系统
- 5G SRM815模组原理框图.jpg
- T型3电平逆变器,lcl滤波器滤波器参数计算,半导体损耗计算,逆变电感参数设计损耗计算 mathcad格式输出,方便修改 同时支持plecs损耗仿真,基于plecs的闭环仿真,电压外环,电流内环
- 毒舌(解锁版).apk
- 显示HEX、S19、Bin、VBF等其他汽车制造商特定的文件格式
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈