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

前端 未结 5 1690
别那么骄傲
别那么骄傲 2021-01-23 05:50

Greetings,

I have recently started to code in C++ and have come across a problem to which I was unable to find the answer, so I thought maybe somebody e

5条回答
  •  一个人的身影
    2021-01-23 06:17

    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.

提交回复
热议问题