All of lore.kernel.org
 help / color / mirror / Atom feed
From: chuckw@ieee.org
To: linux-c-programming@vger.kernel.org
Subject: Sizes of stack variables
Date: Wed, 29 Oct 2003 15:30:41 -0500	[thread overview]
Message-ID: <20031029203041.GA12880@whatever.local> (raw)

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


             reply	other threads:[~2003-10-29 20:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-29 20:30 chuckw [this message]
2003-11-03 16:12 ` Sizes of stack variables nanakos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031029203041.GA12880@whatever.local \
    --to=chuckw@ieee.org \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.