It is hard to imagine writing some low-level C++ core classes without pointers. Pointers and references are everywhere in C++, and they do not work in shared memory! So, if we have a structure like this in shared memory and assign the address of some integer variable in shared memory to pointer_
, the pointer_
would be invalid in other process:
struct with_pointer { int* pointer_; // ... int value_holder_; };
How can we fix that?
The previous recipe is required for understanding this one. The example requires linking against the runtime system library rt
on some platforms.
Fixing it is very simple; we need only to replace the pointer with offset_ptr<>
:
#include <boost/interprocess/offset_ptr.hpp> struct correct_struct { boost::interprocess::offset_ptr<int> pointer_; // ... int value_holder_; };
Now, we are free to use it like a usual pointer:
int main() { boost::interprocess::managed_shared_memory...