How to keep a pointer on a shared data in a boost shared memory segment

Hi everyone ! How to keep a pointer on a shared data in a boost shared memory segment ? I have a function which returns shm.construct<SharedData>(_nameSeg.c_str())(innerDataAllocator); But outside of the function, the object pointed by SharedData is inaccessible My general problem is that I want to have way to get, and set data in shared memory, but without, each time, having to : -get shm -find object in shm based on its name -construct allocators. So I would like to store 1) allocators and 2)a pointer to the shared object How could I do this ? I made a SO question about my problem, can you have a look ? It should be clearer :) https://stackoverflow.com/questions/55195149/how-to-keep-a-pointer-on-a-shar...

On 21/03/2019 01:03, jucin loic wrote:
In general it makes no sense to store pointers inside shared memory, because it is very likely that each process maps the shared memory to different addresses, so a pointer would point to invalid memory in all but one process. To solve this, you store indexes or offsets relative to the base of the shared memory itself (or some other known field within the shared memory) instead of a "real" pointer. Some compilers support a so-called "based pointer", which let you use offsets as if they were pointers, but this is not portable. You can accomplish something similar in portable code by using a smart pointer wrapper type.

Hi,
To solve this, you store indexes or offsets relative to the base of the shared memory itself (or some other known field within the shared memory) instead of a "real" pointer.
I once saw a system, where pointers in shared memory were self-relative. It should be possible to wrap them in a pointer-like class. 73, Mario

Yes, this is a good idea, but you got wrong on my real problem: I agree, and but I don't want to share the pointers between different process. I want to get the pointer to the shared data using managed_shared_memory::construct() or managed_shared_memory::find() and then store this pointer. And do this for of course all different processes. Please, read the SO question, I described there more precisely my problem, and even provided a READY-TO-USE example ( https://stackoverflow.com/questions/55195149/how-to-keep-a-pointer-on-a-shar... ) Thank you in advance ! Le jeu. 21 mars 2019 à 14:10, Klebsch, Mario via Boost-users < boost-users@lists.boost.org> a écrit :
participants (4)
-
Gavin Lambert
-
Ion Gaztañaga
-
jucin loic
-
Klebsch, Mario