r/C_Programming • u/Sad-Finish2729 • May 21 '26
Why does GCC on Windows allocate non-adjacent stack slots for local variables unless & or volatile is used?
#include <stdio.h>
void test01(){
int b = 0x00454647;
/* E F G */
int a = 0x41424344;
/* A B C D */
// printf("%p %p\n", &b, &a); // commented
// volatile int b = ...; // or this
char *p1 = (char *)&a;
printf("%s\n", p1);
}Hi everyone,
I'm learning C on Windows using GCC (MinGW), and I noticed a strange behavior about stack layout.
Observation:
- When I don't take the address of b (&b) and don't use volatile, the variables a and b are not adjacent in memory. When I print the string from &a, I get DCBA followed by garbage.
- As soon as I add printf("%p", &b); or declare b as volatile, a and b become adjacent (differ by exactly 4 bytes), and I get DCBAGEF as expected.
Interestingly:
- Clang (on Windows) allocates them adjacent even without & or volatile.
- GCC on Linux also tends to put them next to each other by default.
Only GCC on Windows (MinGW) shows this "non-adjacent unless address is taken" behavior.
Questions:
- What exactly is GCC doing during stack slot allocation in this case?
- Why does taking the address (&) or using volatile change the layout so dramatically?
- Is this related to some Windows ABI / MinGW specific stack alignment or optimization pass?
- Is there any flag (besides -O0) that can make GCC behave more like Clang in terms of stack layout predictability?
I know that the C standard doesn't guarantee stack layout or variable order, but I'm curious about the implementation difference between GCC (Windows) vs Clang/GCC (Linux).
Any insight into GCC's internals (especially stack slot allocation, assign_stack_local, reload pass, etc.) would be greatly appreciated!
Thanks!
2
Upvotes
4
u/un_virus_SDF May 21 '26
Because a and b doesn't need to live in the stack if you don't.
Your variables lives between the stack and the registers.
volatile means modifiable from external source (mostly threads), which means you need to put them in a shared memory location, a.k.a. the stack.
Same goes for &, you cannot have the address of a register so you move it on the stack.
else the variables are allocated in registers if possible as it allows faster execution and sometimes less instructions.
This is why the class specifier register exists. If you do
int register a =0you cannot dereference it, I don't know about volatile. This keyword enforced keeping the variable in a register.Note that this keyword is not used anymore because compiler can do that alone.
16
u/tstanisl May 21 '26
The layout of unrelated variables is outside of scope of C standard and compilers are not obligated to follow any "adjacency" rules, even for
volatile. Don't rely on it! Pack variables into struct or use assembly (asmstatement) to control it on your own .5
u/flyingron May 21 '26
C doesn't mandate a "stack" period.
Automatic variables are just stored in some memory that is unique to the invocation of the block they are in. While a stack is a common way to implement this, many architecures will store them in registers when this is possible (perhaps backing them up to a stack when they need the registers for something else).
Similarly, push variables on the stack as part of a subroutine linkage isn't necessarily done.
We worked on a machine once (Denelcor HEP supercomputer). It didn't really support the UNIX idea of a stack. There as a single data memory segment per process. Our stacks were linked lists of "stack frames" rather than the normal stack format.
2
u/cafce25 May 21 '26
Look at the assembly, it tells you what this does get compiled to. Most likely it just puts the data on the stack right when it needs to, that is when it calls
printfand doesn't reserve any stack space just for the variablesaandbwhich comfortably fit into a register.1
u/ComradeGibbon May 21 '26