0

In the Boost.Interprocess documentation Where is this being allocated? it is stated that Boost.Interprocess containers are placed in shared memory using two mechanisms at the same time:

  • Boost.Interprocess construct<>, find_or_construct<>... functions. These functions place a C++ object in the shared memory. But this places only the object, not the memory that this object may allocate dynamically.
  • Shared memory allocators. These allow allocating shared memory portions so that containers can allocate dynamically fragments of memory to store newly inserted elements.

What is the use case to have a boost.vector where internal memory lives in the current process, but using a shared memory allocator so that elements are placed in shared memory ?

If I want to share this structure to another process :

struct Shared
{
    vector<string> m_names;
    vector<char>   m_data;
};

I guess I want the vectors to be accessible to the other process so that it can iterate on them, right ?

poukill
  • 540
  • 8
  • 18

2 Answers2

2

find_or_construct and friends are for your own direct allocations.

The allocators are to be passed to library types to do their internal allocations in similar fashion. Otherwise, only the "control structure" (e.g. 16 bytes for a typical std::string) would be in the shared memory, instead of all the related data allocated by the standard library container internally.

sehe
  • 374,641
  • 47
  • 450
  • 633
0

Well, you cannot access the vector as such from the other process but you can access the elements (so in your example the strings) e.g. via a pointer

Tobi
  • 47
  • 4