Do C compilers provide any guarantee on the placement of elements on the c stack such that accessing a[3] in int a[2]; when a is proceeded by int b[2]; for example:
int a[2];
int b[2];
a[3];
Provide access to b[1]; but via using "a"?
Do C compilers provide any guarantee on the placement of elements on the c stack such that accessing a[3] in int a[2]; when a is proceeded by int b[2]; for example:
int a[2];
int b[2];
a[3];
Provide access to b[1]; but via using "a"?
The ordering of local variables on the stack is entirely implementation depend, and can in fact change when more variables are added or removed or when compiler settings are changed.
For example:
#include <stdio.h>
int main()
{
int x;
char y;
double z;
printf("&x=%p\n", (void *)&x);
printf("&y=%p\n", (void *)&y);
printf("&z=%p\n", (void *)&z);
return 0;
}
When compiled on gcc with -O0:
&x=0x7ffd15fa27ec
&y=0x7ffd15fa27eb
&z=0x7ffd15fa27e0
z is first, then y, then x:
When compiled with -O3:
&x=0x7ffd87f4a4c4
&y=0x7ffd87f4a4c3
&z=0x7ffd87f4a4c8
y is first, then x, then z:
So even on the same compiler it can vary.