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