Alex Rivera | Logout

is it possible to place std::vector to shared memory?

Asked 2011-03-19T17:26:56.507
15

I would like to create a std::vector in shared memory using the CreateFileMapping() windows API function. I know how to create shared memory and manage it, but how do I assign std::vector to a fixed address in memory? I cannot use boost or other libraries in my case, I am using CBuilder++ 2010. One variant I think is maybe to use

std::vector<int> myVec; 
myVec *mv;
mv = shared_memory_addr ?

But how do I detect the real size of vectors to resize memory?

Edit
Report

2 Answers

8

I'd use Boost.Interprocess, which has an explanation of how to do just this: http://www.boost.org/doc/libs/1_38_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_interprocess_container

Note that this does not use std::vector<>, which is not suitable for shared-memory use, because it is typically implemented in terms of three pointers (begin, end, capacity, or some equivalents), and addresses will differ between processes. So Boost.Interprocess has its own vector class, which is purpose-built for what you are trying to do.

answered 2011-03-19T17:41:03.643
2

You need to implement your own allocator to achieve that. The allocator is a std::vector<> template parameter.

answered 2011-03-19T17:35:19.667

Your Answer