0

Ok, I have a working program that reads external virtual memory on linux. This is what I want to do: Program 1 is executed and has specific addresses in memory to communicate with program 2. I know that program 2, which is reading and writing values to and from program 1 is working I am stuck with the making of program 2, I have this code:

#include <iostream>
using namespace std;
int main()
{
  int* i;
  i = (int*)0x7ffABCDDCBA1;
  *i = 1;
  cout << *i << " " << i << endl;
}

Note:

this is just a test code to see if it works(it doesn't)

program1 would read 0x7ffABCDDCBA1 at program 2's pid, it compiles just fine, but on executing i get "Segmentation fault"

Note:

#include <iostream>
using namespace std;
int main()
{
  int* i;
  i = (int*)0x7ffABCDDCBA1;
  cout << i << endl;
}

Works just fine

Jdg50
  • 448
  • 2
  • 9
  • 18
Mikdore
  • 699
  • 5
  • 16
  • you should know the difference between *i and i. you may be dereferencing invalid memory address in first code – user2760375 Dec 18 '18 at 16:26
  • Memory addresses in one process have no meaning in another process. That's what virtual addressing is for. – stark Dec 18 '18 at 16:35

1 Answers1

3

You are accessing some random memory location, which most likely does not belong to your process address space and so causes undefined behavior.

You should use shared memory between processes: How to use shared memory in Linux.

There are few other techniques, but this one is most comonnly used for this purpose.

kocica
  • 6,412
  • 2
  • 14
  • 35
  • You're right about that, but i wonder if i could do it this way, this project is just for fun so if there'd be a way to do it like this it'd be nice – Mikdore Dec 18 '18 at 16:34
  • Yes it _might_ work, but as I said - it has Undefined behavior. Thats the reason why it sometimes works, and sometimes do not. – kocica Dec 18 '18 at 16:36
  • No problem, i would recommend you some linux address space related article. Its fun reading – kocica Dec 18 '18 at 16:40