最近我离开学校几天,想在C++中做一个处理内存地址的小程序实验.
我想看看,如果当前正在运行的程序(让它称之为程序A)创建了一个指向堆中int对象的指针,可以被另一个程序看到并进行修改(程序B).
所以对于程序A,这是我的基本代码:
// Program A
#include <iostream>
using namespace std;
int main()
{
// Pointer to an int object in the heap
int *pint = new int(15);
// Display the address of the memory in heap
cout << pint << endl;
// Display the value stored in that address
cout << *pint << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
计划A的产出:
0x641030
15
Run Code Online (Sandbox Code Playgroud)
对于程序B,我查看了如何通过以下链接分配特定的内存地址:http: //www.devx.com/tips/Tip/14104
计划B的代码是:
// Program B
#include <iostream>
using namespace std;
int main()
{
// assign address …Run Code Online (Sandbox Code Playgroud)