使用[]括号访问共享内存中的std :: vector值

时间:2011-03-18 15:25:48

标签: c++ boost

我使用boost shared_memory_manager模板在共享内存中创建向量:

using namespace boost::interprocess;

typedef allocator<nIcon*, managed_shared_memory::segment_manager>  ShmemAllocator;


typedef std::vector<nIcon*, ShmemAllocator> icons_v;
if(already_running) 
{

  managed_shared_memory segment(create_only, "MySharedMemory", 65536);
  const ShmemAllocator alloc_inst (segment.get_segment_manager());
  icons = segment.construct<icons_v>("icons_v")(alloc_inst);
} else {
  managed_shared_memory segment(open_only, "MySharedMemory");
  icons = segment.find<icons_v>("MyVector").first;
}

现在,要访问向量,我不能使用standart []括号,因为编译器说使用。或 - &gt;是错误。我如何访问矢量项?

1 个答案:

答案 0 :(得分:1)

您通常不应该混合容器和共享内存。如果使用容器的两个不同实现构建的进程尝试相互协作,则可能非常危险。

您通常应该选择仅使用原生类型。

相关问题