我想知道是否可以做这样的事情:
I was wondering whether it is possible to do something like this:
unsigned int address = 0x0001FBDC; // Random address :P
int value = *address; // Dereference of address
意思是,是否可以获取内存中特定地址的值?
Meaning, is it possible to get the value of a particular address in memory ?
谢谢
你可以而且应该这样写:
You can and should write it like this:
#include <cstdint>
uintptr_t p = 0x0001FBDC;
int value = *reinterpret_cast<int *>(p);
请注意,除非有某种保证 p 指向整数,否则这是未定义的行为.如果您尝试访问它不希望您访问的地址,则标准操作系统将终止您的进程.但是,这可能是独立程序中的常见模式.
Note that unless there is some guarantee that p points to an integer, this is undefined behaviour. A standard operating system will kill your process if you try to access an address that it didn't expect you to address. However, this may be a common pattern in free-standing programs.
(早期版本的 C++ 应该说 #include 和 intptr_t.)
(Earlier versions of C++ should say #include <stdint.h> and intptr_t.)
这篇关于C++ - 获取特定内存地址的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)