是否可能放置std :: vector到共享内存? [英] is it possible to place std::vector to shared memory?

查看:1339
本文介绍了是否可能放置std :: vector到共享内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会使用CreateFileMapping()Windows API函数在共享内存中创建std :: vector。我知道如何创建共享内存和管理它,但如何放置std :: vector到内存中的固定地址?
我不能使用boost或其他库在我的情况下,我使用CBuilder ++ 2010.
一个变种我认为也许使用

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

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

但是如何检测向量的实际大小以调整内存大小?

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

推荐答案

我将使用Boost.Interprocess,它有如何做到这一点的解释:
http://www.boost.org/doc/libs/1_38_0/doc/html/interprocess/ quick_guide.html#interprocess.quick_guide.qg_interprocess_container

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

请注意,这不会使用 std :: vector<> ,其不适合于共享存储器使用,因为它通常按照三个指针(开始,结束,容量或一些等同物)来实现,并且地址将在进程之间不同。所以Boost.Interprocess有自己的向量类,它是为你想要做的。

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.

这篇关于是否可能放置std :: vector到共享内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆