How to see the content of a specific memory having only the address

Hello all!

First of all... Sorry for my English. :-)

Now to my problem.

I am trying to see if it is possible to read the content of any part of the memory, by giving to a pointer the address of this memory. I know, so I was told, that this possible in Assembler (I have no knowing about Assembler). I wonder if it could be done C and C++ too.

I wrote the two following codes. The first one to write a data to an address, the second one to read it.
Here they are:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Program to write data
// write.cpp
#include <iostream>
#include <stdio.h>

#define MAX 8
int main(void)
{
    int var[MAX]={0};
    int* pvar=nullptr, i__=0;

    for(i__=0; i__<MAX; i__++)
    {
        var[i__]=i__;
        std::cout << &var[i__] << " " << var[i__] << "\n";
        // * Output example:
        /*
            0x6dfec8 0
            0x6dfecc 1
            0x6dfed0 2
            0x6dfed4 3
            0x6dfed8 4
            0x6dfedc 5
            0x6dfee0 6
            0x6dfee4 7
        */

    }

    // * Assigning address of var[6] which stores 7 to the pointer pvar
    pvar = (int *)0x6dfee4;
    // * Here I get the correct output even if I try this Programm many times.
    printf("%i\n", (*pvar));
    // * Output example:
    /*
        7
    */
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Program to read content from an address
// read.cpp

#include <iostream>
#include <stdio.h>

int main(void)
{
    int* ptr = nullptr;
    // * Assigning a real (existing) address of RAM to the pointer ptr
    // * For example 0x6dfee4 where number 7 is stored from the above program.
    ptr = (int*)0x6dfee4;
    // * Trying to see the content.
    printf("ptr points to %i\n", *ptr);
    // * Output example:
    /*
        ptr points to 4354080
    */

  return 0;
}


There suppose to have to be a 7 there instead of 4354080.

Can someone explain what is wrong and how to solve it?

Thank you!!!
Last edited on
Why would there be any particular value at some random address (0x6dfee4)? You are not accessing legal memory. You might just get junk back, your program might crash, or worse: it won't crash.

In any modern OS, all programs use their own virtual address space. The address in one program is not the same as the address in another program, and even if it were, programs are generally isolated so it's illegal to access the memory of another program.

Unless you're programming for very specific hardware, you should not be expected to be allowed to access a random memory address like that.

____________________________________________

To actually access the memory of another program, you need to use the API of whatever OS you're on. This is beyond what I've dabbled in, but you need to set up some sort of hook or special read function.
In Windows, for example, see: https://stackoverflow.com/a/8192189/8690169
Last edited on
> There suppose to have to be a 7 there instead of 4354080.

Try this in program 1.
1
2
3
4
5
6
    int var[MAX]; // no initialiser
    for(i__=0; i__<MAX; i__++)
    {
        std::cout << "Before: " << &var[i__] << " " << var[i__] << "\n";
        var[i__]=i__;
        std::cout << "After: " << &var[i__] << " " << var[i__] << "\n";

What you see in the before case is more akin to what you're seeing in program 2.
Just the random contents of memory before you initialised it to something meaningful.

> Can someone explain what is wrong and how to solve it?
Solve what?

You ran the program twice, and each run begins with a clean sheet of paper (or memory full of zeros).

On modern operating systems, there is no scope at all for looking at your program memory to figure out what program was running previously.

If you want two instances of the program to communicate across time, then the best thing to use is a file.

@Ganado
Thank you, but I'm not asking for that, because I know this.

I'm asking if there is a way to do this supposing the data is still there. In Assambler this is posisible to do. And I know that in C and C++, there is the possibility to read the data of other running program. I want to know how to do this.

Here some example of what I’m trying to do understand:

https://blog.mh-nexus.de/2013/12/reading-process-memory-ram/
https://www.youtube.com/watch?v=Vtlc-WP7iDw
https://blog.holbertonschool.com/hack-the-virtual-memory-c-strings-proc/
Last edited on
That page you linked
https://blog.mh-nexus.de/2013/12/reading-process-memory-ram/
tells you how to do this.

You have to ask the operating system to let you see some memory from a different process. Read the section Reading memory from another process under Windows
Yes, but ver complex!
It requires asking your operating system to let you read the memory of another process.

On windows, you do this with the Win API functions OpenProcess and ReadProcessMemory.
Topic archived. No new replies allowed.