关于这个的有趣讨论开始了here但是没有人能够提供 C++ 的做法:
#include <stdio.h>
int main(void)
{
int* address = (int *)0x604769;
printf("Memory address is: 0x%p\n", address);
*address = 0xdead;
printf("Content of the address is: 0x%p\n", *address);
return 0;
}
在 C++ 中做这种事情最合适的方式是什么?
最佳答案
在 C++ 中,总是更喜欢 reinterpret_cast
而不是 C-cast。它是如此的丑陋,以至于有人会立即发现危险。
例子:
int* ptr = reinterpret_cast<int*>(0x12345678);
那东西伤了我的眼睛,我喜欢它。
关于c++ - 如何在 C++ 中初始化指向特定内存地址的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3934775/