C ++ - 获取特定内存地址的值 [英] C++ - Get value of a particular memory address

查看:286
本文介绍了C ++ - 获取特定内存地址的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以这样做:

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 ?

感谢

推荐答案

#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< stdint.h> intptr_t 。)

(Earlier versions of C++ should say #include <stdint.h> and intptr_t.)

这篇关于C ++ - 获取特定内存地址的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆