The compiler makes absolutely no guarantees about where local variables will be placed on the stack. So you cannot have any expectations other than that some random addresses will be printed to the screen.
As an example, the compiler may see that a
only ever holds the value 22
so it uses an optimisation like rvalue static promotion to turn it into a static
variable, so &a
would then point into your program image's .rodata
section.
It could also realise that some stack space can be saved by reordering a
, b
, t
, and other values (e.g. the stack pointer and return pointer) placed on the stack.
The compiler might also see that comparing the address of two distinct objects on the stack makes no sense (you can only do pointer arithmetic using pointers within the same allocation), and decide to "optimise" the difference to the constant, 42
.
All of these optimisations would apply in C or C++ as well, and are completely legal based on the respective language specs.