我尝试做以下事情:
使用 boost::interprocess 库在共享内存中创建一个“大”数组(1 000 000 + 对象)
我的代码包含以下内容:
managed_shared_memory testarray(create_only, "Test", 45000000);
typedef std::pair<SingleField, uint32_t> TestType;
TestType * test = testarray.construct<TestType>("TestArray")[45000000];
我的问题是:如何确定此 boost 函数的返回类型是什么?
如果我对以下内容执行与上面相同的操作:SingleField 而不是 "::pair 它似乎不起作用,但我不需要第二个容器,我只需要一个,但有一个它不起作用!
eclipse 的输出对我来说有点太神秘了。自从我使用 boost 以来,由于此类问题我已经停止了好几次,有没有一种简单的方法可以弄清楚函数将返回什么“类型”? (我来自 Java,所以我习惯了一些“简单”的定义,它说 Object x )如果我能弄清楚特定函数返回的类型,以及我为自己编写的所有函数,我真的会很高兴这是很简单,但是对于这个库,我似乎有问题。
第二个问题:为什么那些例子总是带有“类型”对,这是库的前置条件吗?
-> 我试过使用 #include ,Eclipse 告诉我它的 std::pair 问题是为什么是 T* ?这是起始段地址吗?
感谢您的宝贵时间和回答。
eclipse 输出:
Multiple markers at this line
- unused variable test
- cannot convert const
boost::interprocess::detail::named_proxy<boost::interprocess::segment_manager<char,
boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,
boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index>, Field, false> to
SharedMemoryInitializer::Create()::TestType* in initialization
我已经多次阅读 boost 库手册,也许我看错了站点或页面,如果您提供我遗漏的信息,我将很高兴。
最佳答案
在我看来,您的代码存在两个主要问题:
您似乎分配了 45000000 个
TestType
类型的对象这可能不是您想要的(除非TestType
每个实例只需要一个字节):TestType * test = testarray.construct<TestType>("TestArray")[45000000];
根据文档 (*),您必须为构造函数调用提供括号(您使用的是第二个版本):
TestType * test = testarray.construct<TestType>("TestArray")[45000000]
<强>()
;
我假设
TestType
有一个无参数的构造函数。
关于c++ - boost 共享内存类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057964/