This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Memory Allocation On Stack
Yeah, that was me who asked it on kerneltrap :)
If it is trying to align ESP on a 16-byte boundary, that seems fine
for a single integer variable or anything less than 16 bytes, but why
is it allocating 116 bytes for 100 byte buffer ? That doesn't fit on a
16-byte boundary.
(gdb) list
1 #include
2
3 int main(int argc, char **argv) {
4 char buf[100];
5
6 return 0;
7 }
8
(gdb) disassemble main
Dump of assembler code for function main:
0x080483a4 : lea 0x4(%esp),%ecx
0x080483a8 : and $0xfffffff0,%esp
0x080483ab : pushl 0xfffffffc(%ecx)
0x080483ae : push %ebp
0x080483af : mov %esp,%ebp
0x080483b1 : push %ecx
0x080483b2 : sub $0x74,%esp
0x080483b5 : mov 0x4(%ecx),%eax
0x080483b8 : mov %eax,0xffffff88(%ebp)
0x080483bb : mov %gs:0x14,%eax
0x080483c1 : mov %eax,0xfffffff8(%ebp)
0x080483c4 : xor %eax,%eax
0x080483c6 : mov $0x0,%eax
0x080483cb : mov 0xfffffff8(%ebp),%edx
0x080483ce : xor %gs:0x14,%edx
0x080483d5 : je 0x80483dc
0x080483d7 : call 0x80482e4 <__stack_chk_fail@plt>
0x080483dc : add $0x74,%esp
0x080483df : pop %ecx
0x080483e0 : pop %ebp
0x080483e1 : lea 0xfffffffc(%ecx),%esp
0x080483e4 : ret
End of assembler dump.
The above listing is same as given on kerneltrap.
On 7/20/07, Andrew Haley <aph-gcc@littlepinkcloud.com> wrote:
Pankaj Kohli writes:
> I wrote a simple C program to test the memory allocation for local
> variables. But when i try to disassemble the program using gdb, it
> shows that 16 bytes are being reserved for local variables on the
> stack, although i declared a single integer variable which is supposed
> to take only 4 bytes.
> Can anyone tell, why it is reserving 16 bytes on the stack for just a
> single variable ?
This question was asked on the kernel list on May 19, 2007. Please
find the answer at http://kerneltrap.org/node/8236
Andrew.
--
- Pankaj
----------------------------------------------------------------------------------------------------------------------
There are only 10 kind of people in the world, those who can count in
binary, and those who cannot.