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 ??