Boost Users :
Subject: Re: [Boost-users] How to keep a pointer on a shared data in a boost shared memory segment
From: Gavin Lambert (boost_at_[hidden])
Date: 2019-03-21 01:38:26
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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net