不能,但我很难解释为什么不能,这问题有点像1+1为什么等于2,显然,但为什么显然是个超难的问题……
Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。
Example:The Valgrind Quick Start Guide
#include <stdlib.h> void f() { int* x = malloc(10 * sizeof(int)); x[10] = 0; // problem 1: heap block overrun } // problem 2: memory leak -- x not freed int main() { f(); return 0; }
Memcheck将打印有关其检测到的内存错误和泄漏的消息如下
➜ Experiment gcc myprog.c -o myprog ➜ Experiment valgrind --leak-check=yes ./myprog ==1360612== Memcheck, a memory error detector ==1360612== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==1360612== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==1360612== Command: ./myprog ==1360612== ==1360612== Invalid write of size 4 ==1360612== at 0x10916B: f (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612== by 0x109185: main (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612== Address 0x4b58068 is 0 bytes after a block of size 40 alloc'd ==1360612== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==1360612== by 0x10915E: f (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612== by 0x109185: main (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612== ==1360612== ==1360612== HEAP SUMMARY: ==1360612== in use at exit: 40 bytes in 1 blocks ==1360612== total heap usage: 1 allocs, 0 frees, 40 bytes allocated ==1360612== ==1360612== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==1360612== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) ==1360612== by 0x10915E: f (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612== by 0x109185: main (in /home/ubuntu/WORKSPACE/Experiment/myprog) ==1360612== ==1360612== LEAK SUMMARY: ==1360612== definitely lost: 40 bytes in 1 blocks ==1360612== indirectly lost: 0 bytes in 0 blocks ==1360612== possibly lost: 0 bytes in 0 blocks ==1360612== still reachable: 0 bytes in 0 blocks ==1360612== suppressed: 0 bytes in 0 blocks ==1360612== ==1360612== For lists of detected and suppressed errors, rerun with: -s ==1360612== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)