【发布时间】:2020-09-27 07:35:16
【问题描述】:
当我发现一些奇怪的事情时,我正在玩弄 Valgrind: 我的 C++ 程序什么都不做,但是有 1 个内存分配和 1 个空闲。
我的简单程序:
int main() {
return 0;
}
使用 g++ 编译并使用 Valgrind 检查时
> g++ main.cpp
> valgrind --leak-check=full --track-origins=yes ./a.out
==40790== Memcheck, a memory error detector
==40790== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==40790== Using Valgrind-3.16.0.GIT and LibVEX; rerun with -h for copyright info
==40790== Command: ./a.out
==40790==
==40790==
==40790== HEAP SUMMARY:
==40790== in use at exit: 0 bytes in 0 blocks
==40790== total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated
==40790==
==40790== All heap blocks were freed -- no leaks are possible
==40790==
==40790== For lists of detected and suppressed errors, rerun with: -s
==40790== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我的问题:我的程序什么都不做。 alloc 和 free 是从哪里来的?
有趣的是,使用 gcc 编译的同一程序显示零分配和释放:
> gcc main.c
> valgrind --leak-check=full --track-origins=yes ./a.out
==40740== Memcheck, a memory error detector
==40740== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==40740== Using Valgrind-3.16.0.GIT and LibVEX; rerun with -h for copyright info
==40740== Command: ./a.out
==40740==
==40740==
==40740== HEAP SUMMARY:
==40740== in use at exit: 0 bytes in 0 blocks
==40740== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==40740==
==40740== All heap blocks were freed -- no leaks are possible
==40740==
==40740== For lists of detected and suppressed errors, rerun with: -s
==40740== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
后续问题:为什么同一段代码的两个内存分配不同?
编译器:gcc (GCC) 10.1.0
valgrind:valgrind-3.16.0.GIT
【问题讨论】:
-
C++ 库分配了一些东西供自己使用,然后将其删除。这里没有发生任何令人兴奋的事情。
-
如果我没有链接或#include 任何东西,那会是什么?
-
您肯定与某些东西相关联:标准 C++ 库。你的编译器为你做了。
-
@Donald 在 main 之前发生了很多事情,如果你好奇的话,可以使用
gdb之类的工具来看看是怎么回事 :)
标签: c++ gcc memory-leaks valgrind