简体   繁体   English

C++ 程序什么都不做,但 Valgrind 显示 memory alloc

[英]C++ program does nothing but Valgrind shows memory alloc

I was toying around with Valgrind, when I noticed something weird: my C++ program does nothing, yet there is 1 memory alloc and 1 free.我正在玩 Valgrind,当我注意到一些奇怪的事情时:我的 C++ 程序什么都不做,但是有 1 个 memory 分配和 1 个空闲。

My simple program:我的简单程序:

int main() {
  return 0;
}

when compiled with g++ and checked with Valgrind使用 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)

My question: My program does nothing.我的问题:我的程序什么都不做。 Where does the alloc and free come from? alloc 和 free 是从哪里来的?

Interestingly enough, the same program compiled with gcc, shows zero allocs and frees:有趣的是,使用 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)

Follow up question: Why do the two memory allocations differ, for the same piece of code?后续问题:为什么对于同一段代码,两个 memory 分配不同?

compiler: gcc (GCC) 10.1.0编译器:gcc (GCC) 10.1.0
valgrind: valgrind-3.16.0.GIT valgrind:valgrind-3.16.0.GIT

The main function is the entry point of your code.主要的 function是您代码的入口点。 It doesn't have to be (and seldom is) the entry point to the process for the operating system that is loading your program.它不一定是(而且很少是)加载程序的操作系统进程的入口点。

There's usually plenty of code running first to set up things needed for the standard library (like setting up the standard I/O streams, and fetching the actual arguments from the operating system) before your main function is called.在调用主 function 之前,通常首先运行大量代码来设置标准库所需的东西(例如设置标准 I/O 流,并从操作系统中获取实际的 arguments)。

And it's important to note that the main function is called like any other function.重要的是要注意, main的 function 与其他任何 function 一样被调用。 Once it returns it will return to the initialization code which will now clean up after itself (like freeing memory it might have allocated, and closing streams, etc.).一旦它返回,它将返回到初始化代码,该代码现在将自行清理(例如释放它可能已分配的 memory,并关闭流等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM