Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

pointers - C++ - Get value of a particular memory address

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 ?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can and should write it like this:

#include <cstdint>

uintptr_t p = 0x0001FBDC;
int value = *reinterpret_cast<int *>(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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...