0

I'm accessing the same memory address with two separate instances of eclipse C++ and I'm getting different results. Why is this happening?

I am running two different instances of eclipse for C++ at the same time. The first instance (Testrun.cpp) is assigning a value to the memory, then printing the memory address then stopping because it is waiting for user input. I am taking the printed memory address and assigning it to a pointer in the second instance (Hello.cpp). The second instance is accessing the memory address and not printing the integer that was assigned by the Testrun.cpp instance.

Why is this happening? I will like to write software that will access memory of another application that is running.

// Testrun.cpp
#include <iostream>
using namespace std;

int Tito(int g);

int main()
{
    int l;
    int *e;
    int i = 11;
    //int *p = (int *)0x73fe44;
    e = &i;
    cout << &i << endl; // prints !!!Hello World!!!
    //cout << e << endl;
    //*e = 4;
    //int *p = (int *)0x28ff43;
    //*p = 99;
    //cout << i << endl;
    cout << "Hello" << endl;
    cin >> l;
    cout << *e << endl;
    return 0;
}

int Tito(int g)
{
    return g;
}



// Hello.cpp
#include <iostream>
using namespace std;

int main()
{
    //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    int i = 23;
    int *g;
    g = &i;
    *g = 232;
    int *p = (int *)0x73fe40;
    cout << *p << endl;
    return 0;
}
Tito
  • 102
  • 9
  • 2
    You can't. Use https://www.boost.org/doc/libs/1_55_0/doc/html/interprocess/sharedmemorybetweenprocesses.html – Passer By Jan 30 '19 at 06:55
  • 4
    Your question starts out as the classic [XY Problem](http://xyproblem.info/). -- *I will like to write software that will access memory of another application that is running.* -- Now this is what you should have mentioned first. You can't achieve this by what you're trying to do. – PaulMcKenzie Jan 30 '19 at 06:58
  • 1
    Possible duplicate of [How does a debugger work?](https://stackoverflow.com/questions/216819/how-does-a-debugger-work) – Raedwald Jan 30 '19 at 07:01
  • 2
    Each process has it's own virtual memory. They cannot be shared! – Wei Guo Jan 30 '19 at 07:05
  • For your requirement you need to learn about shared memory mapping [mmap](http://man7.org/linux/man-pages/man2/mmap.2.html) – sameerkn Jan 30 '19 at 07:58

0 Answers0