C++ C++;程序除了Valgrind显示内存分配外什么也不做

C++ C++;程序除了Valgrind显示内存分配外什么也不做,c++,gcc,memory-leaks,valgrind,C++,Gcc,Memory Leaks,Valgrind,我在玩弄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== Copyrigh

我在玩弄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编译的同一个程序显示零allocs和frees:

> 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是代码的入口点。它不一定(而且很少)是加载程序的操作系统进程的入口点

在调用主函数之前,通常会先运行大量代码来设置标准库所需的内容(例如设置标准I/O流,并从操作系统获取实际参数)


重要的是要注意,
main
函数的调用与任何其他函数一样。一旦返回,它将返回初始化代码,现在将清理它自己(比如释放内存,它可能已经分配,关闭流等)。< /p> C++库分配了一些东西供自己使用,并删除它。没有什么激动人心的事情发生了。如果我没有链接或包含任何东西,那会是什么?你肯定与一些东西相关:标准C++库。你的编译器为你做了。@Donald在main之前发生了很多事情,如果你好奇的话,你可以使用像
gdb
这样的工具来看看发生了什么:)最后一段的第一句话有点笨拙-
main()
不像其他函数那样被调用。它只从“大量代码”调用,它为程序设置了条件,C++标准明确禁止从任何其他代码调用它。是的,一些实现可以将它称为任何其他函数(即,那些特定的实现, MIN())/Cux>被调用为任何其他函数,但是C++标准不需要这样做。