Two different values at the same memory address

前端 未结 7 795
春和景丽
春和景丽 2020-11-22 08:14

Code

#include 
using namespace std;

int main() {
    const int N = 22;
    int * pN = const_cast(&N);
    *pN = 33;
    co         


        
7条回答
  •  星月不相逢
    2020-11-22 08:57

    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.

提交回复
热议问题