#include <iostream>
int main(int argc, char *argv[]) {
int x = 3;
int y;
int *px = &x;
y = *px--;
*px = 5;
std::cout << "&x = " << &x << " px = " << px << " &px = " << &px << "&y =" << &y; // strange line
std::cout << "x = " << x << " y = " << y << std::endl;
std::cin.get();
}
When I use &y in "strange line", system prints out x = 3, y = 5, this can be explained, because in actual stack memory, x's address in highger place, y's address in lower place (y's address = x'address - 4), so *px = 5 change y's value;
BUT! Something very strange happens, when I use y instead of &y in "strange line", x = 3 and y = 3! What happend? It seems that even I use "g++ -O0", nothing changes, so it's not compiler's fault, so why? Picture following: