The C standard doesn't specify the layout of variables on the stack but I want to understand how local variables are arranged by the gcc
compiler. A simple example is given below
void fun(int a, int b, int c, int d, char *pass) {
int flag = 0x7a69;
char buffer[10];
strcpy(buffer, pass);
}
int main(int argc, char **argv) {
fun(1, 2, 3, 4, argv[1]);
}
No matter what the order of buffer
& flag
, the flag
always comes before the buffer
.
(gdb) p &buffer
$1 = (char (*)[10]) 0xffffcff2
(gdb) p &flag
$2 = (int *) 0xffffcfec
I compile the program as:
gcc -g -m32 test.c
I tried to reorder the declaration of buffer
and flag
in order to see their arrangement on the stack frame but the order was always the same.
- By looking at the source code, can i guess the order of locals on stack-frame by the
gcc
? - On what criteria
gcc
order the locals and why?