valgrind使用小测试

一、测试代码

// 常见内存问题
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    char *ptr = malloc(10);
    ptr[12] = 'a';// 内存越界
    memcpy(ptr+1, ptr, 5);// 踩内存
    char a[10];
    a[12] = 'i';// 数组越界
    free(ptr);
    free(ptr);// 重复释放
    char *p1;
    *p1 = '1';// 非法指针

    return 0;

}

二、测试结果

root@meituanyun:~/mycode# valgrind --leak-check=full ./test4
==26072== Memcheck, a memory error detector
==26072== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==26072== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==26072== Command: ./test4
==26072== 
==26072== Invalid write of size 1
==26072==    at 0x400663: main (test4.c:8)
==26072==  Address 0x520404c is 2 bytes after a block of size 10 alloc'd
==26072==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26072==    by 0x400656: main (test4.c:7)
==26072== 
==26072== Source and destination overlap in memcpy(0x5204041, 0x5204040, 5)
==26072==    at 0x4C32513: memcpy@@GLIBC_2.14 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26072==    by 0x400681: main (test4.c:9)
==26072== 
==26072== Invalid free() / delete / delete[] / realloc()
==26072==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26072==    by 0x40069D: main (test4.c:13)
==26072==  Address 0x5204040 is 0 bytes inside a block of size 10 free'd
==26072==    at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26072==    by 0x400691: main (test4.c:12)
==26072==  Block was alloc'd at
==26072==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26072==    by 0x400656: main (test4.c:7)
==26072== 
==26072== Use of uninitialised value of size 8
==26072==    at 0x4006A2: main (test4.c:15)
==26072== 
==26072== Invalid write of size 1
==26072==    at 0x4006A2: main (test4.c:15)
==26072==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==26072== 
==26072== 
==26072== Process terminating with default action of signal 11 (SIGSEGV)
==26072==  Access not within mapped region at address 0x0
==26072==    at 0x4006A2: main (test4.c:15)
==26072==  If you believe this happened as a result of a stack
==26072==  overflow in your program's main thread (unlikely but
==26072==  possible), you can try to increase the size of the
==26072==  main thread stack using the --main-stacksize= flag.
==26072==  The main thread stack size used in this run was 8388608.
==26072== 
==26072== HEAP SUMMARY:
==26072==     in use at exit: 0 bytes in 0 blocks
==26072==   total heap usage: 1 allocs, 2 frees, 10 bytes allocated
==26072== 
==26072== All heap blocks were freed -- no leaks are possible
==26072== 
==26072== For counts of detected and suppressed errors, rerun with: -v
==26072== Use --track-origins=yes to see where uninitialised values come from
==26072== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值