且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在两个进程之间共享指针?

更新时间:2022-05-16 09:02:12

您不能.否则,任何人都可以更改任何其他正在运行的应用程序的数据.想象一下,潜伏在您的内存中寻找信用卡号怎么办,等等...

指针指向其值仅在实际为其分配值的进程内有效的地址.
另一个进程(无论与第一个进程是否相同)都会看到不同的地址映射,甚至可能具有指向同一地址的指针,从而有效地指向完全不同的变量.

您可以在两个进程之间使用共享内存",但是它将由不同的指针表示.
要处理这些内容,您必须使用进程间通信 [ ^ ]
You cannot. Otherwise anyone can alter the data of whatever other running applications. Imagine what can I do by lurking your memory seeking for credit card numbers, and so on...

A pointer points to an address whose value is valid only inside the process that actually allocated it.
Another process (no matter if is identical or not to the first) see a different address map, and may even have pointers to a same address effectively pointing to completely different variables.

You can use "shared memory" between two processes, but it will be represented by different pointers.
To deal with this stuff you have to work with Interprocess communication[^]