[Boost-users] How to keep a pointer on a shared data in a boost shared memory segment

15 views
Skip to first unread message

jucin loic via Boost-users

unread,
Mar 20, 2019, 9:22:24 AM3/20/19
to boost...@lists.boost.org, jucin loic
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-shared-data-in-a-boost-shared-memory-segment

Gavin Lambert via Boost-users

unread,
Mar 20, 2019, 9:38:43 PM3/20/19
to boost...@lists.boost.org, Gavin Lambert
On 21/03/2019 01:03, jucin loic wrote:
> 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 ?

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.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users

Klebsch, Mario via Boost-users

unread,
Mar 21, 2019, 3:10:31 AM3/21/19
to Osman Zakir via Boost-users, Klebsch, Mario
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

jucin loic via Boost-users

unread,
Mar 23, 2019, 12:40:15 PM3/23/19
to boost...@lists.boost.org, jucin loic
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-shared-data-in-a-boost-shared-memory-segment
 Thank you in advance !

Ion Gaztañaga via Boost-users

unread,
Mar 23, 2019, 2:49:22 PM3/23/19
to jucin loic via Boost-users, Ion Gaztañaga
I'm afraid I don't understand the problem, and your minimal example
needs password. Could you send a single cpp file with the minimal code
to understand your question?

Best,

Ion

jucin loic via Boost-users

unread,
Mar 24, 2019, 3:48:06 AM3/24/19
to boost...@lists.boost.org, jucin loic, Ion Gaztañaga
Yes, forgot to give the password, it's simply: stackoverflow
In the minimal example is only a signle cpp, that you can also find attached. 

Thanks :)

Jucin Loic
main.cpp
Reply all
Reply to author
Forward
0 new messages