I have read, it is undefined in some C standards (perhaps 99?) what happens when a const is modified. But a student presented me with some code, which I modified.
I cannot see anything special about the address of the constant variable a
. I verified that &a
and b
are the same, so the compiler is not subtly pointing to some other location.
Yet when I assign *b
, the const value does not change.
I am not running with optimization. When I compile with the -g
flag to debug and step into the code, I get the results I expect (the memory location of the variable a
changes). Yet the code presented below does not reflect the updated value of a
.
Is this that temps are now being placed in registers even in debug mode, with no optimization?
#include <iostream>
using namespace std;
int main(){
const int a = 15;
cout << a << '\n';
int * b= (int*)&a;
cout << &a << "\n";
cout << b << "\n";
*b = 20;
cout << *b << '\n';
cout << a << '\n';
int x = a;
cout << x << '\n';
x = *b;
cout << x << '\n';
return 1;
}