为什么堆栈上分配了这么多空间?

Why is so much space allocated on the stack?(为什么堆栈上分配了这么多空间?)

本文介绍了为什么堆栈上分配了这么多空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自回答堆栈溢出问题为什么书上说编译器为内存中的变量分配空间"?,我试图向 OP 演示在堆栈上分配变量时会发生什么,以及编译器如何生成知道要分配的内存大小的代码.显然编译器分配的空间比需要的多.

This question comes from answering Stack Overflow question Why do books say, "the compiler allocates space for variables in memory"?, where I tried to demonstrate to the OP what happens when you allocate a variable on the stack and how the compiler generates code that knows the size of memory to allocate. Apparently the compiler allocates much more space than what is needed.

但是,在编译以下内容时

However, when compiling the following

#include <iostream>
using namespace std;

int main()
{
    int foo;
    return 0;
}

在调试模式下编译 Visual C++ 2012 后,您将获得以下汇编器输出,但未进行任何优化:

You get the following assembler output with Visual C++ 2012 compiled in debug mode with no optimisations on:

int main()
{
00A31CC0  push        ebp
00A31CC1  mov         ebp,esp
00A31CC3  sub         esp,0CCh  // Allocates 204 bytes here.
00A31CC9  push        ebx
00A31CCA  push        esi
00A31CCB  push        edi
00A31CCC  lea         edi,[ebp-0CCh]
00A31CD2  mov         ecx,33h
00A31CD7  mov         eax,0CCCCCCCCh
00A31CDC  rep stos    dword ptr es:[edi]
   int foo;
   return 0;
00A31CDE  xor         eax,eax
}

在我的程序中再添加一个 int 会使上面的注释行变为以下内容:

Adding one more int to my program makes the commented line above to the following:

00B81CC3  sub         esp,0D8h // Allocate 216 bytes

@JamesKanze 在我上面链接的答案中提出的问题是,为什么编译器,而且显然不仅仅是 Visual C++(我还没有用另一个编译器做过实验),分别分配了 204 和 216 字节,在第一种情况只需要 4 个,第二种情况只需要 8 个?

The question raised by @JamesKanze in my answer linked atop, is why the compiler, and apparently it's not only Visual C++ (I haven't done the experiment with another compiler), allocated 204 and 216 bytes respectively, where in the first case it only needs four and in the second it needs only eight?

此程序创建一个 32 位可执行文件.

This program creates a 32-bit executable.

从技术角度来看,为什么需要分配 204 个字节而不是 4 个字节?

From a technical perspective, why may it need to allocate 204 bytes instead of just 4?

调用两个函数并在main中创建一个double和两个int,得到

Calling two functions and creating a double and two int in main, you get

 01374493  sub         esp,0E8h  // 232 bytes

对于与上述编辑相同的程序,它在发布模式下执行此操作(无优化):

For the same program as the edit above, it does this in release mode (no optimizations):

 sub    esp, 8                // Two ints
 movsd  QWORD PTR [esp], xmm0 // I suspect this is where my `double` goes

推荐答案

这个额外的空间是由/Zi 编译选项产生的.这启用了编辑 + 继续.额外的空间可用于在调试时编辑代码时可能添加的局部变量.

This extra space is generated by the /Zi compile option. Which enables Edit + Continue. The extra space is available for local variables that you might add when you edit code while debugging.

你也看到了/RTC 的效果,它将所有局部变量初始化为 0xcccccccc 以便更容易诊断由于忘记初始化变量而导致的问题.当然,这些代码都不是在默认的 Release 配置设置中生成的.

You are also seeing the effect of /RTC, it initializes all local variables to 0xcccccccc so that it is easier to diagnose problems due to forgetting to initialize variables. Of course none of this code is generated in the default Release configuration settings.

这篇关于为什么堆栈上分配了这么多空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么堆栈上分配了这么多空间?

基础教程推荐