版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/90311664
#include<iostream> #include<stdlib.h> using namespace std; void func(){ int *x=(int *)malloc( 10 * sizeof ( int ) ) ; x[10]=0; } int main(){ func(); cout<<"done"<<endl; return 0; }
[root@localhost charpter05]# g++ -g 0508test.cpp -o 0508 [root@localhost charpter05]# ./0508 done
[root@localhost charpter05]# valgrind ./0508 ==16719== Memcheck, a memory error detector ==16719== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==16719== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==16719== Command: ./0508 ==16719== ==16719== Invalid write of size 4 ==16719== at 0x4008DE: func() (0508test.cpp:6) ==16719== by 0x4008EE: main (0508test.cpp:9) ==16719== Address 0x5a1a068 is 0 bytes after a block of size 40 alloc'd ==16719== at 0x4C29EC3: malloc (vg_replace_malloc.c:309) ==16719== by 0x4008D1: func() (0508test.cpp:5) ==16719== by 0x4008EE: main (0508test.cpp:9) ==16719== done ==16719== ==16719== HEAP SUMMARY: ==16719== in use at exit: 40 bytes in 1 blocks ==16719== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==16719== ==16719== LEAK SUMMARY: ==16719== definitely lost: 40 bytes in 1 blocks ==16719== indirectly lost: 0 bytes in 0 blocks ==16719== possibly lost: 0 bytes in 0 blocks ==16719== still reachable: 0 bytes in 0 blocks ==16719== suppressed: 0 bytes in 0 blocks ==16719== Rerun with --leak-check=full to see details of leaked memory ==16719== ==16719== For lists of detected and suppressed errors, rerun with: -s ==16719== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==16719== Memcheck, a memory error detector ==16719== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==16719== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==16719== Command: ./0508
==16719== Invalid write of size 4 ==16719== at 0x4008DE: func() (0508test.cpp:6) ==16719== by 0x4008EE: main (0508test.cpp:9)
==16719== LEAK SUMMARY: ==16719== definitely lost: 40 bytes in 1 blocks ==16719== indirectly lost: 0 bytes in 0 blocks ==16719== possibly lost: 0 bytes in 0 blocks ==16719== still reachable: 0 bytes in 0 blocks ==16719== suppressed: 0 bytes in 0 blocks
文章来源: https://blog.csdn.net/chengqiuming/article/details/90311664