2

I am running two separate programs simultaneously.

*In the first one , I am allocating an integer dynamically and retrieving its address.

*In the second one while the first is still running, I am using the address generated in first one to access the value at the allocated pointer in the first program in the second program.

But the second program always crashes.

My question is - Can I access a variable of one program from another program using pointers?
They should be accessible by going to their addresses. Shouldn't they?

Here is the code of first program:-

//Program 1
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
  int *p;
  p= new int; //allocating an integer
  *p=15;      //setting up a value.
  getch();
  int x=(int)p;  //retrieving the address and converting it to decimal system.
  cout<<*p<<endl<<p<<endl<<x; //printing assigned value and address to use in second program
  getch();
  delete p;
 }

The output is this...

 15
 0xfc13a8
 16520104

Now, while its still running(pointers not deleted yet , the holding off with getch function) I start the second program whose code is this..

 //Program 2
 #include<iostream>
 #include<conio.h>

 using namespace std;

 int main()
 {
  int *p;
  int x;
  cout<<"Enter an address:-";
  cin>>x;
  cout<<endl;
  p=(int *)x;
  cout<<*p;
  getch();
 }

It asks for the address and I enter the address 16520104 given out by first program and I try to display the assigned value at that address but the program always crashes?? Why so ??

anjanik012
  • 149
  • 3
  • 11
  • In most modern operating systems, each process runs in its own virtual memory address space, and there is no direct way for one process to access the memory of another process. You could however use an appropriate shared memory API for inter-process communication. – Paul R Aug 29 '16 at 09:28
  • In most modern systems, each process gets its own virtual memory address space. So "it depends". – juanchopanza Aug 29 '16 at 09:28
  • interesting research for you will be "shared memory", "interprocess communication", "mmap" – Richard Hodges Aug 29 '16 at 09:29
  • What's the target platform? Does it employ virtual addressing? – Sergey Kanaev Aug 29 '16 at 09:36
  • My OS is Windows 10 64 bit @SergeyKanaev – anjanik012 Aug 29 '16 at 09:37
  • If you need to communicate between 2 processes some data you need to use shared memory or some other form of Inter-process-communication in windows. As stated in several answers each process has it's own address space so the address is only valid inside of the one program. – Hayt Aug 29 '16 at 09:41
  • @anjanik012 Well, Windows does employ virtual addressing, so you'll have to use some shared memory mechanisms from your OS. The code you've posted could work for MS-DOS as it doesn't use any address virtualization. – Sergey Kanaev Aug 29 '16 at 10:41
  • @SergeyKanaev, you'll need a multi tasking ms-dos to get two programs executing at the same time, either. – Luis Colorado Sep 01 '16 at 08:05
  • @LuisColorado for sure ;-) What I wanted to say is the the OS should not have address virtualization to allow direct data access between distinct processes – Sergey Kanaev Sep 02 '16 at 16:59

5 Answers5

2

For most platforms, memory for Processes are isolated from each other (different address space).

And besides, even if they were in the same process (or your platform allows processes to read of each other's memory), what you are doing is Undefined behavior because:

There's no guarantee that a pointer will fit into an int. The type that is guaranteed to hold numeric pointer values is std::uintptr_t. (of cause, you could use other types with larger width).


If you are using any of these popular platforms, then Process are isolated by default. To quote from Wikipedia:

Notable operating systems that support process isolation:

  • Unix, Linux, OS X
  • VMS
  • Microsoft Windows from Windows NT 3.1

What you are most likely looking for is some form of Interprocess Communication

Community
  • 1
  • 1
WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
1

The addresses are virtual addresses, valid for only a specific program. The OS maps the virtual addresses to physical address.

See virtual address space.

robor
  • 2,969
  • 2
  • 31
  • 48
1

Each program runs in owns virtual address space your first program terminated after executing main method. then there is no use of address of a variable in program 1. You are getting run time error or segmentation fault because you are try to access a location which is pointed by another program1. if you are try communicate between two program or two process use inter process communication mechanism like Socket,Message Queue etc. for more detail about inter process communication https://en.wikipedia.org/wiki/Inter-process_communication

Abhishek
  • 379
  • 1
  • 8
  • Not, you are getting the run time error because that address is not allocated in the second program. Other program address spaces are not mapped anywhere in one process virtual address space. Address `0x12345678` in process A can be a complete different thing in process B or can be nothing(and in this case the operating system complaints) You could get a valid address, but with completely different contents. You *are not trying to access an address that belongs to a different process*. You are trying to access a local address with no previous knowledge of what it is used for. – Luis Colorado Sep 01 '16 at 08:08
0

Both will be running as a separate process and will have their own address space.

And It will be a segmentation fault when you try to access out of your address space.

Shankar Shastri
  • 1,134
  • 11
  • 18
0

My question is - Can I access a variable of one program from another program using pointers?

Yes, if you're using an OS that doesn't use virtual memory.

They should be accessible by going to their addresses. Shouldn't they?

Not in modern OSes with virtual memory. Every application has its own virtual address space, address 0x1234 in program A is not the same 0x1234 in program B. You need to use IPC or any other shared storage mechanism in order to communicate between running programs.

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
  • Direct memory using OS might not be sufficient for the OP's programs to work. The OS might use another mechanism to protect the memory of a process to prevent other processes from accessing it. – eerorika Aug 29 '16 at 09:40