For the code shown below, If I print the addresses, I get the following.
&test_var1 = 0x7fff0067d87c
&barrier (passed argument) = 0x7fff0067d770
&i (passed argument) = 0x7fff0067d77c
&test_var2 = 0x7fff0067d78c
There are two things I don’t understand here. First is that I read that C pushes arguments from right to left, then how &i is greater than &barrier. Knowing that stack grows from a higher address to a lower address, &i should be at a lower address. Moreover, the local variable test_var2 is at even a greater address.
Secondly, one would expect the value of &barrier and &test_var1 to be close together, but no, you see a big difference of 268 bytes. What is the stack holding in between?
Note that I’m using optimization O3. Is this due to that? Maybe the compiler has played some tricks here? I use volatile to make sure every variable is on the stack here and not cached in some register.
void some_func()
{
.........
{
volatile int test_var1 = 0;
}
call_func( i, &barrier );
........
}
void call_func( volatile int i, volatile pthread_barrier* barrier )
{
volatile int test_var2 = 0;
........
}
On x86 the stack (used when f() calls g()) grows downward.
Anyhow the way the compiler arranges the var/s for a certain call is implementation dependend.