This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Memory Allocation On Stack
On 7/20/07, Pankaj Kohli <pankaj208@gmail.com> wrote:
Hi,
0x08048352 <main+14>: sub $0x10,%esp
The above instruction allocates the memory on the stack... 16 bytes in
this case.
i tried the same thing for prog:
#include <stdio.h>
int main(int argc, char **argv) {
int x=10;
int b=9;
return 0;
}
and it returned:
08048324 <main>:
8048324: 8d 4c 24 04 lea 0x4(%esp),%ecx
8048328: 83 e4 f0 and $0xfffffff0,%esp
804832b: ff 71 fc pushl 0xfffffffc(%ecx)
804832e: 55 push %ebp
804832f: 89 e5 mov %esp,%ebp
8048331: 51 push %ecx
8048332: 83 ec 10 sub $0x10,%esp
so again 16 byts for 2 integers .....
8048335: c7 45 f4 0a 00 00 00 movl $0xa,0xfffffff4(%ebp)
804833c: c7 45 f8 09 00 00 00 movl $0x9,0xfffffff8(%ebp)
8048343: b8 00 00 00 00 mov $0x0,%eax
8048348: 83 c4 10 add $0x10,%esp
804834b: 59 pop %ecx
804834c: 5d pop %ebp
804834d: 8d 61 fc lea 0xfffffffc(%ecx),%esp
8048350: c3 ret
8048351: 90 nop
8048352: 90 nop
so its that the stack is probably 16 byte alligned not the integer ....
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
> >
>
--
- Pankaj