I have created an integer variable using the following code in first.cpp :
#include <iostream>
using namespace std;
int main()
{
int myvar = 10;
cout << &myvar;
// I only need two steps above.
// The following steps are coded to make this program run continuously.
cout << "Enter your name" << endl;
string name;
cin >> name;
return 0;
}
Output of first.cpp is:
>0x6dfed4
While the above program is running, I also run the following program in second.cpp :
#include <iostream>
using namespace std;
int main()
{
// I this program I want to read the content in myvar variable in first.cpp
// How to do it ?
}
Using the second program, I want to read the content of the myvar
variable in first.cpp.
How to do it?
Thank you.
To elaborate on my comment above:
As I wrote each program will run in a different process . Each process has a separate adddress space . Therefore using the address of a variable in another process doesn't make any sense.
In order to communicate between processes, you need some kind of IPC (inter-process communication): Inter-process communication
If you only need to share a variable, the first mechanism that comes to mind is to use shared memory : Shared memory
Shared-memory is very much OS dependent . You didn't mention which OS you are using. On Windows you can use Win API for managing shared memory: Windows shared memory
There's an equivalent on Linux, but I am not familiar with the details.
Alternatively you can use the boost solution which is cross platform: boost shared memory
If you'll delve into that it will be clear that these kind of solutions comes with some compilcations .
So the question is why do you need to do it? Maybe there's a better solution to your problem (you haven't described what it actually is, so there's not much more I can say).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.