为什么在堆栈上分配如此多的空间? [英] Why is so much space allocated on the stack?

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

问题描述

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



但是,当编译以下



  #include< iostream> 
using namespace std;

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

在调试模式下使用Visual C ++ 2012编译时,优化:

  int main()
{
00A31CC0 push ebp
00A31CC1 mov ebp ,esp
00A31CC3 sub esp,0CCh //此处分配204个字节。
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 到我的程序使上面的注释行如下:

  00B81CC3 sub esp, 0D8h //分配216字节

@JamesKanze在我的回答链接的问题,编译器,显然它不只是Visual C ++(我没有做另一个编译器的实验),分别分配204和216字节,其中在第一种情况下,它只需要四个,在第二个只需要八个



这个程序创建了一个32位的可执行文件。



从技术角度看,为什么需要分配204个字节只需4个



编辑:



调用两个函数并创建一个 / code>和两个 int ,您会得到

  01374493 sub esp,0E8h // 232 bytes 

对于与上述编辑相同的程序,这在释放模式(无优化):

  sub esp,8 //两个int 
movsd QWORD PTR [esp ],xmm0 //我怀疑这是我的双重


解决方案>

此额外空间由/ Zi编译选项生成。其中启用编辑+继续。额外的空间可用于在调试时编辑代码时可能添加的局部变量。



您还可以看到/ RTC的效果,它将所有本地变量初始化为0xcccccccc,以便更容易诊断由于忘记初始化变量而导致的问题。当然,在默认的发布配置设置中不会生成此代码。


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;
}

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
}

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

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

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?

This program creates a 32-bit executable.

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

EDIT:

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

解决方案

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.

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆