Boost logo

Boost Users :

Subject: Re: [Boost-users] How can I use unique_ptr with container and string in boost::interprocess?
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2008-10-21 12:41:39


blp330 wrote:
> Hi,
>
> I have a complex data structure that wants to put into shared memory.
> In STL way it should be:
>
> ...
> In this way, I always must construct a SharedMemoryString first then put it
> into container:
> SharedMemoryString mStr(charAlloc);
> mStr = "some stirng...";
> queue->push_back(mStr);
>
> 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
> scope.

I have no compiler at hand to test the unique_ptr issue, but you can use
move semantics to avoid memory allocations:

//transfer resources from mStr to the newly
//created string
queue->push_back(move(mStr));

should work. Also:

SharedMemoryString mStr2(charAlloc);
queue->push_back(mStr2);
queue.back().swap(mStr);

Regards,

Ion


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