This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Stack allocation
- From: Alexandru Juncu <alexj at rosedu dot org>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 18 Nov 2011 17:03:45 +0200
- Subject: Stack allocation
- References: <CAPhGq=bY8hS0DF2rf7_5E8ycYS52uJR8UH=Yjb0NiDBdaSR+6Q@mail.gmail.com>
Hello!
[I sent an email on the gcc main list by my mistake, and I am moving
the discussion here]
I have a curiosity with something I once tested. I took a simple C
program and made an assembly file with gcc -S.
The C file looks something like this:
int main(void)
{
 int a=1, b=2;
 return 0;
}
The assembly instructions look like this:
subl  Â$16, %esp
movl  Â$1, -4(%ebp)
movl  Â$2, -8(%ebp)
The subl $16, means the allocation of local variables on the stack,
right? 16 bytes are enough for 4 32bit integers.
If I have 1,2,3 or 4 local variables declared, you get those 16 bytes.
If I have 5 variables, we have "    Âsubl  Â$32, %esp". 5,6,7,8 variables ar
the same. 9, 10,11,12, 48 bytes.
The observation is that gcc allocates increments of 4 variables (if
they are integers). If I allocate 8bit chars, increments of 16 chars.
So the allocation is in increments of 16 bytes no matter what.
OK, that's the observation... my question is why? What's the reason
for this, is it an optimization (does is matter what's the -O used?)
or is it architecture dependent (I ran it on x86) and is this just in
gcc, just in a certain version of gcc or this is universal?
I got a response that is related to the cache line alignment, to
optimize cache hits.
But I tried to compile the program with the --param l1-cache-size and
got the same .s file. Is this ok?
Thank you!
--
Alexandru Juncu
ROSEdu