Two different values at the same memory address

Code

#include <iostream>
using namespace std;

int main() {
    const int N = 22;
    int * pN = const_cast<int*>(&N);
    *pN = 33;
    cout << N << '\t' << &N << endl;
    cout << *pN << '\t' << pN << endl;
}

Output

22 0x22ff74

33 0x22ff74

Why are there two different values at the same address?


Solution 1:

Why are there two different datas at the same address?

There aren't. The compiler is allowed to optimize any mention of a const to be as though you had written its compile-time value in there.

Note that the compiler is also allowed to generate code that erases your hard disk when you run it if you do nasty tricks like writing to memory reserved for consts.

Solution 2:

You get undefined behavior on the line *pN = 33;, because you're modifying a const value. Anything can happen. Don't do it.


Likely, though, your compiler simply optimized. In the line:

cout << N << '\t' << &N << endl;

It knows N is a constant expression with the value 22, so just changes the line to:

cout << 22 << '\t' << &N << endl;

And on your next line, you fetch the value at the address of N, which you "set" to 33. (But really, all you did was remove any guarantees about the state of your program.)

Solution 3:

By stating that N is const, you have promised that you won't modify it. And then you go and modify it. This breaks one of the assumptions the compiler is making, and as a result, the program behaves incorrectly.

This is referred to as "undefined behavior" - after violating an assumption in the language, the behavior of the program is completely undefined. It need not have produced that output - it could've produced 33 for both, or 42, or crashed, or erased your hard drive, or summoned demons through your nasal passages. So, don't modify const values :)

Solution 4:

int * pN = const_cast<int*>(&N);
*pN = 33;

Your code invokes Undefined Behavior1 because you are modifying the content of a const qualified variable/object.

1) Undefined Behavior: Behavior, such as might arise upon use of an erroneous program construct or of erroneous data, for which the Standard imposes no requirements.[Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

Solution 5:

You can declare N as volatile, to force the compiler to fetch the current value from the variable's memory location.

volatile const int N = 22;