29、valgrind 进行new/malloc内存检查

基本思想:沉迷于valgrind无法自拔,回顾一下new/malloc的内存申请和释放的方法;

#include
#include
#include
using namespace std;
#define N 10
int main()
{

        int *a=new int[N];
        for(int i=0;i

内存检查:
 

ubuntu@ubuntu:~/test$ g++ -g t.cpp
ubuntu@ubuntu:~/test$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --show-reachable=yes --log-file=a.log  ./a.out
ubuntu@ubuntu:~/test$ cat a.log
==32770== Memcheck, a memory error detector
==32770== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32770== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==32770== Command: ./a.out
==32770== Parent PID: 37391
==32770==
==32770==
==32770== HEAP SUMMARY:
==32770==     in use at exit: 72,704 bytes in 1 blocks
==32770==   total heap usage: 25 allocs, 24 frees, 73,744 bytes allocated
==32770==
==32770== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==32770==    at 0x4C2DBF6: malloc (vg_replace_malloc.c:299)
==32770==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==32770==    by 0x40106C9: call_init.part.0 (dl-init.c:72)
==32770==    by 0x40107DA: call_init (dl-init.c:30)
==32770==    by 0x40107DA: _dl_init (dl-init.c:120)
==32770==    by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==32770==
==32770== LEAK SUMMARY:
==32770==    definitely lost: 0 bytes in 0 blocks
==32770==    indirectly lost: 0 bytes in 0 blocks
==32770==      possibly lost: 0 bytes in 0 blocks
==32770==    still reachable: 72,704 bytes in 1 blocks
==32770==         suppressed: 0 bytes in 0 blocks
==32770==
==32770== For counts of detected and suppressed errors, rerun with: -v
==32770== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

 

你可能感兴趣的:(C/C++基础知识)