开发者

How to retrieving variable value in C++ if you know the variable address

开发者 https://www.devze.com 2023-02-18 05:23 出处:网络
Greetings, I have recently st开发者_如何学编程arted to code in C++ and have come across a problem to which I was unable to find the answer, so I thought maybe somebody else might know the answer.

Greetings,

I have recently st开发者_如何学编程arted to code in C++ and have come across a problem to which I was unable to find the answer, so I thought maybe somebody else might know the answer.

Is it possible to retrieve a variable value from another program if you know a variable address? Imagine that I have a memory address displayed in a program, something like: 0x7fff5fbff758 and I would like (in my own program which is not related to the first one) to get the value stored in that memory address.

Is that possible? If so, could somebody please explain me how. Thank you in advance.


On today operating systems, the programs handle virtual addresses, not physical ones. Shortly, a specific address for one programs will not point to the same physical location for other programs.

To do what you want on modern operating systems, you can, for instance, set up a shared memory location.

But there is a lot of easier way to pass a value from one program to another.

If you are just wondering that out of curiosity, that's a good question, you can look at what "virtual memory" is.


C++ has no comment on this, one way or the other. It depends entirely on the platform on which your program is running. If you're using Windows, for example, you can use the ReadProcessMemory() function to read the memory of another process (assuming you have adequate permissions).

Note that modern operating systems are designed to protect processes from interfering with each other. One of the ways they do this is by giving each process its own address space. Processes can't access memory outside this space without using special APIs.


It is possible, but it is OS-specific (there is no common C support for it). In general, your second program needs to have the permission that debugger has, and use the same kind of OS calls that a debugger uses.


On most modern general-purpose OSes (Windows, Linux, etc), you cannot do that. Different programs run in different processes, and each process has its very own memory space. Address 0x7fff5fbff758 in one probably points to a very different place in RAM than address 0x7fff5fbff758 in another (if that address even exists in the other).

This is why modern OSes have interprocess communications mechanisims, like pipes, shared memory, COM, etc.


Edit: This obsolete, because the question has changed.

If you know the type of the variable, its possible.

For an int variable, you need to insert lines like

int* addr = (int*)0x7fff5fbff758;
std::cout << *addr << std::endl;

somewhere in the affected program.

Accessing the variable from a different program is generally not easily done in a modern operating system, each process has it's own address space so the same address in different processes may map to different physical memory location.

It depends on the OS, for example in linux you need to trace a process if you want to do it from a different process, see man ptrace. You can read the data in this case with PTRACE_PEEKDATA.

0

精彩评论

暂无评论...
验证码 换一张
取 消