代码
#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;
}
输出
22 0x22ff74
33 0x22ff74
为什么同一个地址有两个不同的值?
最佳答案
Why are there two different datas at the same address?
没有。编译器可以优化任何提及 const 的内容,就好像您已将其编译时值写入其中一样。
请注意,如果您执行了诸如写入为常量保留的内存等令人讨厌的技巧,编译器还可以生成在运行时删除硬盘的代码。
关于c++ - 同一内存地址处有两个不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530432/