(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
Mark the third assembler instruction, it allocated 16 bytes instead of the expected 12 bytes. Why is this? I thought the 3rd line highlights automatic variables ...
Even if I deleted the destination, the distribution will still be 16 bytes.
Thanks.
Edit
// no header. nothing int main() { int a = 1; int b = 10; int c; c = a + b; return 0; }
g ++ -g -o demo demo.cpp
Next ... I read a couple more threads on stack alignment (sorry, now I'm studying computer architecture and organizational class ... so I'm not familiar with this at all)
Laying and leveling the stack
I assume this is a compiler setting. Therefore, the default value is at least 16 bytes.
If we have
int a = 1; int b = 10; int c = 10; int d = 10;
Prior to int d, we will have exactly 16 bytes, and the distribution is still 0x10. But when we give another indent, int e = 10, esp now allocates 32 bytes (0x20).
This shows that esp, the stack pointer, is used only for automatic variables.
Follow-up 2
Call stack and frame stack
Every frame of the stack
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.
But after we allocated int pass-through int d, it already took 16 bytes. Main has no function parameters, therefore zero. But has the line returned where this information went?