memory allocation of local variables

Discussion in 'C Programming' started by sandy, Dec 13, 2006.

  1. sandy

    sandy Guest

    Hi all,
    I tried compiling the above two programs :
    on x86, 32 bit machines.

    And when I used objdump on that I saw the following code.
    Can anyone help me know,
    Why in the objdump of our first program the esp is decremented by 18H
    bytes
    and in the second program the esp is decremented by 28H bytes.

    How actually is teh memory allocated by gcc for local variables.

    Kindly help.

    int main()
    {
    char x;
    return 0;

    }


    [root@localhost ~]# gcc test.c
    [root@localhost ~]# objdump -S a.out | less
    08048348 <main>:
    8048348: 55 push %ebp
    8048349: 89 e5 mov %esp,%ebp
    804834b: 83 ec 18 sub $0x18,%esp
    804834e: 83 e4 f0 and $0xfffffff0,%esp
    8048351: b8 00 00 00 00 mov $0x0,%eax
    8048356: 83 c0 0f add $0xf,%eax
    8048359: 83 c0 0f add $0xf,%eax
    804835c: c1 e8 04 shr $0x4,%eax
    804835f: c1 e0 04 shl $0x4,%eax
    8048362: 29 c4 sub %eax,%esp
    8048364: b8 00 00 00 00 mov $0x0,%eax
    8048369: c9 leave
    804836a: c3 ret
    804836b: 90 nop

    int main()
    {
    double x,y,z;
    char p,q,r;
    return 0;

    }

    08048348 <main>:
    8048348: 55 push %ebp
    8048349: 89 e5 mov %esp,%ebp
    804834b: 83 ec 28 sub $0x28,%esp
    804834e: 83 e4 f0 and $0xfffffff0,%esp
    8048351: b8 00 00 00 00 mov $0x0,%eax
    8048356: 83 c0 0f add $0xf,%eax
    8048359: 83 c0 0f add $0xf,%eax
    804835c: c1 e8 04 shr $0x4,%eax
    804835f: c1 e0 04 shl $0x4,%eax
    8048362: 29 c4 sub %eax,%esp
    8048364: b8 00 00 00 00 mov $0x0,%eax
    8048369: c9 leave
    804836a: c3 ret
    804836b: 90 nop




    --
    Regards,
    Sandeep
     
    sandy, Dec 13, 2006
    #1
    1. Advertising

  2. In article <>,
    sandy <> wrote:

    >Why in the objdump of our first program the esp is decremented by 18H
    >bytes
    >and in the second program the esp is decremented by 28H bytes.
    >
    >How actually is teh memory allocated by gcc for local variables.


    For details of gcc, you should try a gcc-related group.

    But you might find it enlightening to print out the addresses of the
    variables (using the "%p" format in printf()). I didn't.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Dec 13, 2006
    #2
    1. Advertising

  3. "sandy" <> wrote in message
    news:...
    > I tried compiling the above two programs :
    > on x86, 32 bit machines.
    >
    > And when I used objdump on that I saw the following code.
    > Can anyone help me know,
    > Why in the objdump of our first program the esp is decremented by
    > 18H bytes and in the second program the esp is decremented by
    > 28H bytes.
    >
    > How actually is teh memory allocated by gcc for local variables.


    How any particular implementation accomplishes that is off-topic here;
    all comp.lang.c cares about is that the Standard guarantees that it
    happens somehow -- and that's all you need to know to write portable
    code. If you start digging into what happens under the hood, you're
    going to end up writing non-portable code.

    <OT>In this particular case, GCC is setting up the stack frame to hold a
    certain number of bytes of local variables, including padding. If you
    don't know what a stack frame is, or don't know why modifying ESP
    affects it, you're probably better off asking in comp.lang.asm.x86.</OT>

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Stephen Sprunk, Dec 13, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    26
    Views:
    733
    Anonymous 7843
    Jun 17, 2005
  2. gamja
    Replies:
    4
    Views:
    323
    Christian Bau
    Sep 30, 2005
  3. Ken
    Replies:
    24
    Views:
    3,930
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,018
    chris
    Oct 28, 2005
  5. Rahul
    Replies:
    22
    Views:
    832
    Richard Herring
    Feb 13, 2008
Loading...

Share This Page