Varying allocation size for stack variables...

  • Follow


Hey there, i've had a question just buggin the heck out of me lately,
and if anyone could answer my question it would then be off my head
and make me feel an awful lot better :)

While reading articles I often come accross things as the such:

void function(int a, int b, int c) {
   char buffer1[5];
   char buffer2[10];
}

void main() {
  function(1,2,3);
}

YES THIS IS AN ASM QUESTION... don't look away yet..
Then, the ASM for function() starts as the following:       
        
        pushl %ebp
        movl %esp,%ebp
        subl $20,%espe this:

Which makes sense, buffer1 would be 8 bytes and buffer2 would be 12,
making it 20 bytes.. Here is my output where the temp binary is the
compiled code from above.

kspecial@xzziroz:~$ objdump -d temp | grep -A 5 function
080483c0 <function>:
 80483c0:       55                      push   %ebp
 80483c1:       89 e5                   mov    %esp,%ebp
 80483c3:       83 ec 28                sub    $0x28,%esp
 80483c6:       c9                      leave  

Ok? So how come i'm getting 40 bytes allocated for the 20 above that
other people are getting? I realize compilers might vary and also
kernel version, and maybe this is compiler/os specific memory being
allocated...But i'm looking for what specific reasons specificly! The
best you could do is point me to an online article that would point
the (obvious?) out, because I can't find one. I've tried searching for
'gcc memory allocation' and 'gcc local variable allocation'  with no
luck.
Thanks for any help you might give.

--Danny Biggle

0
Reply spamtrap 10/11/2004 4:54:24 AM

Danny Biggle wrote:

> Hey there, i've had a question just buggin the heck out of me lately,
> and if anyone could answer my question it would then be off my head
> and make me feel an awful lot better :)
> 
> While reading articles I often come accross things as the such:
> 
> void function(int a, int b, int c) {
>    char buffer1[5];
>    char buffer2[10];
> }
> 
> void main() {
>   function(1,2,3);
> }
> 
> YES THIS IS AN ASM QUESTION... don't look away yet..
> Then, the ASM for function() starts as the following:       
>         
>         pushl %ebp
>         movl %esp,%ebp
>         subl $20,%espe this:
> 
> Which makes sense, buffer1 would be 8 bytes and buffer2 would be 12,
> making it 20 bytes.. Here is my output where the temp binary is the
> compiled code from above.
> 
> kspecial@xzziroz:~$ objdump -d temp | grep -A 5 function
> 080483c0 <function>:
>  80483c0:       55                      push   %ebp
>  80483c1:       89 e5                   mov    %esp,%ebp
>  80483c3:       83 ec 28                sub    $0x28,%esp
>  80483c6:       c9                      leave  
> 
> Ok? So how come i'm getting 40 bytes allocated for the 20 above that
> other people are getting? I realize compilers might vary and also
> kernel version, and maybe this is compiler/os specific memory being
> allocated...But i'm looking for what specific reasons specificly! The
> best you could do is point me to an online article that would point
> the (obvious?) out, because I can't find one. I've tried searching for
> 'gcc memory allocation' and 'gcc local variable allocation'  with no
> luck.

It has to do with stack alignment (0x28 is a multiple of 8).

See -Os (optimize for size) and -mpreferred-stack-boundary=num

http://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/i386-and-x86-64-Options.html

-- 
Regards, Grumble

0
Reply Grumble 10/11/2004 8:20:46 AM


> It has to do with stack alignment (0x28 is a multiple of 8).
> 
> See -Os (optimize for size) and -mpreferred-stack-boundary=num
> 
> http://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/i386-and-x86-64-Options.html

I greatly appreciate your answer, sorry I couldn't respond earlier I
was working all today, but this makes me happy.. now I know *what* to
read on, that can sometimes be the hardest part of learning I think.

0
Reply spamtrap 10/12/2004 1:43:57 AM

2 Replies
135 Views

(page loaded in 0.114 seconds)


Reply: