Boost logo

Boost Users :

Subject: [Boost-users] How can I use unique_ptr with container and string in boost::interprocess?
From: blp330 (blp330_at_[hidden])
Date: 2008-10-21 10:41:02


I have a complex data structure that wants to put into shared memory.
In STL way it should be:

list<tuple<string, string, map<string, string>>>

As you can see, there are a lot of strings which will be put into shared
My current way to a string into a container is (I use list<string> as a

typedef allocator<char, managed_windows_shared_memory::segment_manager>
typedef basic_string<char, std::char_traits<char>, CharAllocator>

typedef allocator<SharedMemoryString,
typedef list<PushValue, PushValueAllocator> PushQueue;

In this way, I always must construct a SharedMemoryString first then put it
into container:
SharedMemoryString mStr(charAlloc);
mStr = "some stirng...";

In my understanding, this will cost twice memory allocations to store this
string into shared memory.
First time is mStr = "some string...", second time is that mStr is put into
list container, then mStr's memory will be destructed after leaving the
This means if the string length is 1KB, my shared memory size at least must
be 2 * 1KB + container overhead. This seems stupid because twice memory
allocation and copy operation.

So, I want to use unique_ptr to solve this problem. I try:

typedef managed_unique_ptr<SharedMemoryString,
managed_windows_shared_memory>::type StringUniquePtr;

typedef allocator<StringUniquePtr,
typedef list<PushValue, PushValueAllocator> PushQueue;

SharedMemoryString* mStr = new SharedMemoryString(charAlloc);
*mStr = "some stirng...";

queue->push_back(StringUniquePtr(make_managed_unique_ptr(mStr, segment)));

But it can't compile... :(

I have no idea to make unique_ptr to work with string in container in
How do I make this work?

Thank you very much.

View this message in context:
Sent from the Boost - Users mailing list archive at

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at