繁体   English   中英

Boost 共享 Memory 类型

[英]Boost Shared Memory Type

我尝试做以下事情:

使用 boost::interprocess 库在 Shared memory 中创建一个“大”数组(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 function 的返回类型是什么?

如果我用下面的方法在上面做同样的事情:SingleField 而不是“::pair 它似乎不起作用,但我不需要第二个容器,我只需要一个但有一个它不起作用!

eclipse 的 output 对我来说有点太神秘了。 自从我使用 boost 以来,由于此类问题我已经停止了好几次,有没有一种简单的方法可以弄清楚 function 会返回什么“类型”? (我来自 Java,所以我习惯了一些“简单”的定义,上面写着 Object x )如果我能弄清楚特定 function 返回的类型,以及我为自己编写的所有函数,我真的会很高兴这是很简单,但是对于这个库,我似乎有问题。

第二个问题:为什么那些例子总是带有“类型”对,它是库的前提条件吗?

-> 我试过使用#include,Eclipse 告诉我它的 std::pair 问题是为什么它是 T*? 这是起始段地址吗?

感谢您的时间和回答。

Eclipse output:

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 库手册,也许我看错了站点或页面,如果您提供我错过的信息,我会很高兴。

http://www.boost.org/doc/libs/1_42_0/doc/html/interprocess/quick_guide.html#interprocess.quick_guide.qg_interprocess_container

在我看来,您的代码存在两个主要问题:

  • 您似乎正在分配 45000000 个TestType类型的对象,这可能不是您想要的(除非TestType每个实例只需要一个字节):

    TestType * test = testarray.construct<TestType>("TestArray")[45000000];

  • 根据文档 (*),您必须为构造函数调用提供括号(您使用的是第二个版本): TestType * test = testarray.construct<TestType>("TestArray")[45000000] () ;

    我假设TestType有一个无参数的构造函数。

(*) http://www.boost.org/doc/libs/1_46_1/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.allocation_types

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM