C ++ - get the value of a specific memory address

I was wondering if one could do something like this:

unsigned int address = 0x0001FBDC; // Random address :P
int value = *address; // Dereference of address

Sense, is it possible to get the value of a specific address in memory?

thank

+5
source share
1 answer

You can and should write it like this:

#include <cstdint>

uintptr_t p = 0x0001FBDC;
int value = *reinterpret_cast<int *>(p);

Note that if there is some guarantee that ppoints to an integer, this behavior is undefined. A standard operating system will kill your process if you try to access an address that he did not expect from you. However, this may be a general scheme in free-running programs.

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

+14

All Articles