Memory Allocation On Stack
Tim Prince
tprince@myrealbox.com
Fri Jul 20 14:07:00 GMT 2007
Pankaj Kohli wrote:
> Hi,
>
> 0x08048352 <main+14>: sub $0x10,%esp
> The above instruction allocates the memory on the stack... 16 bytes in
> this case.
>
>
> On 7/20/07, kanishk rastogi <kanishk.85@gmail.com> wrote:
>> On 7/20/07, Pankaj Kohli <pankaj208@gmail.com> wrote:
>> > Hi,
>> >
>> > 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 ? Is it some kind of optimization done by gcc ?
>> > Does the exact value depends on the operating system and/or gcc
>> version ?
>> >
>> > #include <stdio.h>
>> > int main(int argc, char **argv) {
>> > int x=10;
>> >
>> > return 0;
>> > }
>> >
>> > Breakpoint 1, main () at test7.c:4
>> > 4 int x=10;
>> > (gdb) disassemble main
>> > Dump of assembler code for function main:
>> > 0x08048344 <main+0>: lea 0x4(%esp),%ecx
>> > 0x08048348 <main+4>: and $0xfffffff0,%esp
>> > 0x0804834b <main+7>: pushl 0xfffffffc(%ecx)
>> > 0x0804834e <main+10>: push %ebp
>> > 0x0804834f <main+11>: mov %esp,%ebp
>> > 0x08048351 <main+13>: push %ecx
>> > 0x08048352 <main+14>: sub $0x10,%esp
>> > 0x08048355 <main+17>: movl $0xa,0xfffffff8(%ebp)
>> > 0x0804835c <main+24>: mov $0x0,%eax
>> > 0x08048361 <main+29>: add $0x10,%esp
>> > 0x08048364 <main+32>: pop %ecx
>> > 0x08048365 <main+33>: pop %ebp
>> > 0x08048366 <main+34>: lea 0xfffffffc(%ecx),%esp
>> > 0x08048369 <main+37>: ret
>> > End of assembler dump.
>> >
>> why do u say that its allocating 16 byte for integer
>>
>> > --
>> > - Pankaj
>> >
>>
OP may have used gcc options which preserve stack alignment, so that
parallel sse code could be used in callees. There are options to break
this; see your "friendly manual."
More information about the Gcc-help
mailing list