0

So, this question is linked to a previous one (std::string without copying from char*).

Now I've come across boost:interprocess::string, which allows the construction of strings inside a shared memory address space. Since I've already got my char* memory, I was wondering if I could reuse the boost:interprocess::string to avoid copying.

However, every single document I'm reading uses boost::interprocess::managed_shared_memory and its siblings.

Is anyone using a boost managed string without shared memory? How?

Thanks!

senseiwa
  • 2,369
  • 3
  • 24
  • 47

2 Answers2

0

Short answer: No.

Longer answer: You must know about your std::string implementation. See for example here for libcxx by LLVM. You can get your char* in there but it might involve ugly hacks and you must adapt your code for any STL implementation.

Albert
  • 65,406
  • 61
  • 242
  • 386
0

The problem is your char * points to the memory of your process ( heap ), when process no longer running this memory is invalid. Shared memory is shared between processes. You have to copy from char * into shared memory segment, in my opinion it's the only option. Please tell me where your char * points to on heap ?

But reading from shared memory or memory mapped file can be done without copying , you can use containers like string_view either boost::string_ref or boost::string_view or in C++17 std::string_view include <experimental/string_view>. These are containers without ownership and allow you to refer to memory by {char * , size} , however since shared memory is shared it would be hard to imagine if it could ever be production quality code since any process can change your memory, unless you never write but only read from SHM. In my humble opinion the answer is NO. However I will wait for more experienced developers.