Assigning a specific memory address from another program and changing its value

Recently, I had free time at school for several days and wanted to do a little experiment in C ++ dedicated to the memory address.

I would like to see that if the current running program (let it call program A) that created a pointer to an int object on the heap, you can see another program and change it (program B).

So, for program A, this is my base code:

// Program A
#include <iostream>

using namespace std;

int main()
{
    // Pointer to an int object in the heap
    int *pint = new int(15);

    // Display the address of the memory in heap
    cout << pint << endl;

    // Display the value stored in that address
    cout << *pint << endl;

    return 0;
}

Output for program A:

0x641030
15

In program B, I looked at how to assign a specific memory address from this link: http://www.devx.com/tips/Tip/14104

Code for program B:

// Program B
#include <iostream>

using namespace std;

int main()
{
    // assign address 0x641030 to p
    int *p = reinterpret_cast< int* > (0x641030);

    cout << p << endl;
    cout << *p << endl;

    return 0;
}

Output for program B:

0x641030
... "Crash"

I do not quite understand. I was expecting 15 of to display *p, but it did what I did not expect.

*p , *p = 2000, , .

, A (cout << &pint;) B (cout << &p;), .

- , ? , . , , , ++/C?

** ** , , Window 7 Professional

+3
3

, . , B A.

. Win32 , WriteProcessMemory, . , Win32 , Unix "mmap" "shmem".

+9

, , ( ) , , - ( ) ( ). , , , o/s . - , .

, , . !

+2

Source: https://habr.com/ru/post/1730346/


All Articles