gpt4 book ai didi

c++ - 同一个内存地址有两个不同的值

转载 作者:IT老高 更新时间:2023-10-28 22:16:41 29 4
gpt4 key购买 nike

代码

#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 的任何提及,就好像您在其中编写了它的编译时值一样。

请注意,如果您执行诸如写入为 consts 保留的内存等讨厌的技巧,编译器也可以在运行时生成删除硬盘的代码。

关于c++ - 同一个内存地址有两个不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3593687/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com