All of lore.kernel.org
 help / color / mirror / Atom feed
* Sizes of stack variables
@ 2003-10-29 20:30 chuckw
  2003-11-03 16:12 ` nanakos
  0 siblings, 1 reply; 2+ messages in thread
From: chuckw @ 2003-10-29 20:30 UTC (permalink / raw)
  To: linux-c-programming

Hey All,
  I was just trying to minimize the size of a program the other day,
and I noticed that the size of the varibles created on the stack is
not necessarily the size they should be.  So, for instance, the 
following code: 

void main()
{
  char buffer[8];

  printf("%s\n", buffer);
}

produces the following assembly:
   ...
8  main:
9    pushl %ebp
10   movl  %esp, %ebp
11   subl  $24, %esp
12   andl  $-16, %esp
13   movl  $0, %eax
     ...

No problem right, the buffer variable is created on the stack, and
I am assuming that the number 24 being subtracted from the stack pointer
is the 8 bytes for the buffer + 16 bytes program overhead(environment, etc).
Now, if I change the source to the following:

void main()
{
  char buffer[16];
   
  printf("%s\n", buffer);
}

produces the following assembly:
   ...
   8  main:
   9    pushl %ebp
   10   movl  %esp, %ebp
   11   subl  $40, %esp
   12   andl  $-16, %esp
   13   movl  $0, %eax
   ...

Shouldn't the stack size only increase to 32 which is 16 bytes of overhead
plus the 16 bytes for the variable?

If someone has some insight into this I would be much abliged.

Chuck


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Sizes of stack variables
  2003-10-29 20:30 Sizes of stack variables chuckw
@ 2003-11-03 16:12 ` nanakos
  0 siblings, 0 replies; 2+ messages in thread
From: nanakos @ 2003-11-03 16:12 UTC (permalink / raw)
  To: chuckw; +Cc: linux-c-programming

Hi the stack size depends on the optimization of the compiler ( gcc )
.Just read the stack frame that is used from the compiler.You can find
such info in http://gcc.gnu.org or sth similar.

Best regards,
Chris.



> Hey All,
>   I was just trying to minimize the size of a program the other day,
> and I noticed that the size of the varibles created on the stack is
> not necessarily the size they should be.  So, for instance, the
> following code:
>
> void main()
> {
>   char buffer[8];
>
>   printf("%s\n", buffer);
> }
>
> produces the following assembly:
>    ...
> 8  main:
> 9    pushl %ebp
> 10   movl  %esp, %ebp
> 11   subl  $24, %esp
> 12   andl  $-16, %esp
> 13   movl  $0, %eax
>      ...
>
> No problem right, the buffer variable is created on the stack, and
> I am assuming that the number 24 being subtracted from the stack pointer
> is the 8 bytes for the buffer + 16 bytes program overhead(environment,
> etc).
> Now, if I change the source to the following:
>
> void main()
> {
>   char buffer[16];
>
>   printf("%s\n", buffer);
> }
>
> produces the following assembly:
>    ...
>    8  main:
>    9    pushl %ebp
>    10   movl  %esp, %ebp
>    11   subl  $40, %esp
>    12   andl  $-16, %esp
>    13   movl  $0, %eax
>    ...
>
> Shouldn't the stack size only increase to 32 which is 16 bytes of overhead
> plus the 16 bytes for the variable?
>
> If someone has some insight into this I would be much abliged.
>
> Chuck
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-11-03 16:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-10-29 20:30 Sizes of stack variables chuckw
2003-11-03 16:12 ` nanakos

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.