为什么这个源代码分配16个字节?

Cpp*_*ner 6 c++ gdb

(gdb) disas /m main
Dump of assembler code for function main():
2   {
   0x080483f4 <+0>: push   %ebp
   0x080483f5 <+1>: mov    %esp,%ebp
   0x080483f7 <+3>: sub    $0x10,%esp

3       int a = 1;
   0x080483fa <+6>: movl   $0x1,-0x4(%ebp)

4           int b = 10;
   0x08048401 <+13>:    movl   $0xa,-0x8(%ebp)

5           int c;
6           c = a + b;
   0x08048408 <+20>:    mov    -0x8(%ebp),%eax
   0x0804840b <+23>:    mov    -0x4(%ebp),%edx
   0x0804840e <+26>:    lea    (%edx,%eax,1),%eax
   0x08048411 <+29>:    mov    %eax,-0xc(%ebp)

7           return 0;
   0x08048414 <+32>:    mov    $0x0,%eax

8   }
   0x08048419 <+37>:    leave  
Run Code Online (Sandbox Code Playgroud)

注意第三个汇编指令,它分配了16个字节而不是预期的12个字节.这是为什么?我认为第3行是分配自动变量......

即使我删除了赋值,分配仍然是16个字节.

谢谢.


编辑

// no header. nothing
int main()
{
        int a = 1;
        int b = 10;
        int c;
        c = a + b;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

g ++ -g -o demo demo.cpp


跟进...我在堆栈对齐上读了几个线程(对不起,我现在正在研究计算机拱和组织课......所以我根本不熟悉这个)

堆栈分配填充和对齐

我认为这是编译器的设置.所以默认情况下,最小值是16字节.

如果我们有

int a = 1;
int b = 10;
int c = 10;
int d = 10;
// --
int e = 10;
Run Code Online (Sandbox Code Playgroud)

直到int d,我们将具有正好16个字节,并且分配仍然是0x10.但是当我们给出另一个选择时,int e = 10,esp现在被分配32个字节(0x20).

这告诉我esp,堆栈指针,仅用于自动变量.


后续行动2

调用堆栈和堆栈帧

每个堆栈框架

Storage space for all the automatic variables for the newly called function.

The line number of the calling function to return to when the called function returns.

The arguments, or parameters, of the called function.
Run Code Online (Sandbox Code Playgroud)

但是在我们通过int d分配了int之后,它已经花了我们16个字节.Main没有函数参数,因此为零.但回归的路线,这些信息去了哪里?

Mys*_*ial 9

我虽然还没有看到源代码,main()我相信这是由于堆栈对齐.

在您的设置下,堆栈可能需要与8个字节对齐.因此esp增加16个字节而不是12个字节.(即使12个字节足以容纳所有变量)

在其他系统(使用SSE或AVX)上,堆栈需要与16或32字节对齐.