linux命令如下:valgrind --tool=memcheck --leak-check=full ./a.out
若没有valgrind工具,ubuntu下需要sudo apt-get install valgrind
如下:
举例说明:
 #include <iostream>
using namespace std; int main(int argc, char *argv[])
{
string* s = new string("hello world");
cout<<*s<<endl; int* m = new int();
cout<<*m<<endl; delete m; return ;
} //running:
nol@nol-VirtualBox:~/desktop$ valgrind --tool=memcheck --leak-check=full ./a.out
==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.11. and LibVEX; rerun with -h for copyright info
==== Command: ./a.out
====
hello world ====
==== HEAP SUMMARY:
==== in use at exit: , bytes in blocks
==== total heap usage: allocs, frees, , bytes allocated
====
==== bytes in blocks are definitely lost in loss record of
==== at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==== by 0x400C40: main (in /home/nol/desktop/a.out)
====
==== LEAK SUMMARY:
==== definitely lost: bytes in blocks
==== indirectly lost: bytes in blocks
==== possibly lost: bytes in blocks
==== still reachable: , bytes in blocks
==== suppressed: bytes in blocks
==== Reachable blocks (those to which a pointer was found) are not shown.
==== To see them, rerun with: --leak-check=full --show-leak-kinds=all
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )
nol@nol-VirtualBox:~/desktop$
 
 
 
 
05-08 08:28