c++ - boost 共享内存类型

标签 c++ boost types boost-interprocess

我尝试做以下事情:

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

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

关于c++ - boost 共享内存类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10057964/

相关文章:

Python,将列表的所有条目从字符串转换为 float

PostgreSQL 将列从 inet 转换为文本

android - Android 智能手机和其他设备之间的直接 Wifi 通信

c++ - 奇怪的列表打印功能行为。如果我打印 "\n"则有效,如果我删除它则无效

c++ - 通过指向基类的指针删除没有新成员的派生类

c++ - Boost Spirit : Error C2664, 无法将 'const boost::phoenix::actor<Eval>' 转换为 'char'

scala - Scala 的 "type"关键字是什么意思?

c++ - 在类中重载运算符并返回对私有(private)值的引用

python - Boost::Python Extract -- 在 C++ 中访问 Python 字符串

c++ - Visual Studio 中的 CMake 项目 : How to add additional include and library directories?