简体   繁体   English

在共享内存中保存函数指针

[英]Save function pointer in shared memory

我知道使用共享内存的多个应用程序可以保存和使用数据。我想知道是否可以将函数指针保存在共享内存中?我的目的是从我的共享对象回调到具有相同功能的函数不同应用程序的返回值和参数。

No.不。

Unless you have somehow loaded your code into the shared memory region, or otherwise caused the code to be loaded at the same address in both processes, the function pointer will not be valid.除非您以某种方式将代码加载到共享内存区域,或者以其他方式导致代码在两个进程中加载​​到相同的地址,否则函数指针将无效。

除非您完全确定自己在做什么,否则将函数指针保存在共享内存段中对我来说听起来是个坏主意,您最终可能会访问您的进程没有特权的函数指针,并且可能最终崩溃你的程序。

It is not guaranteed that a shared library will be loaded to the same address for two different applications.对于两个不同的应用程序,不能保证共享库将加载到相同的地址。 Even worse, a library could be loaded to any random address due to address space layout randomization.更糟糕的是,由于地址空间布局随机化,库可以加载到任何随机地址。 Hence, a function pointer is not invariant and hence can't be stored anywhere.因此,函数指针不是不变的,因此不能存储在任何地方。

As solution, you could create a function table with all possible callbacks in both processes and store offset to a function in shared memory.作为解决方案,您可以创建一个函数表,其中包含两个进程中所有可能的回调,并将偏移量存储到共享内存中的函数。 For sure, those tables should be exactly the same.当然,这些表应该完全相同

不,不能共享函数指针,如果需要,您需要制作该函数的库并在您的过程中使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM