包含字符串的结构的 Boost 进程间向量是否需要特殊的分配器?
Does Boost Interprocess vector of struct containing strings require special allocator?
我正在使用 Boost Interprocess 在共享内存中创建一个 interprocess::vector。我模板化了我的类,以便我可以在内存中存储任何类型的对象:
using namespace boost::interprocess;
template<typename T>
struct MySharedData
{
using ShmemAllocator = allocator<T, managed_shared_memory::segment_manager>;
using MyVector = vector<T, ShmemAllocator>;
MySharedData()
{
segment.reset(new managed_shared_memory(open_or_create, "memory", 10000));
const ShmemAllocator alloc_inst(segment->get_segment_manager());
vec = segment->find_or_construct<MyVector>("vector")(alloc_inst);
}
//...
//...
MyVector* vec{nullptr};
std::unique_ptr<managed_shared_memory> segment{nullptr};
}
我通过简单地调用来添加元素:
vec->push_back(element);
在测试期间,我使用 int 模板化了我的类,效果很好。但是,当我后来使用我的复杂对象时,包括:
struct Complex
{
std::string x;
std::string y;
double a;
int b;
};
在遍历向量并访问元素后不久,我遇到了分割错误:
std::vector<T> read()
// Mutex and condition variable stuff here
std::vector<T> toReturn;
for(auto& t : *vec)
{
toReturn.push_back(t);
}
return toReturn;
}
我正在阅读此页面:
https://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/allocators_containers.html
它提到"容器的容器"要求元素类型具有自己的分配器。
我的 Complex 结构也需要这个吗,因为字符串使我的向量成为容器(字符(的容器,它能解释为什么我在迭代元素时遇到分段错误吗?
我的 Complex 结构也需要这个吗,因为字符串使我的向量成为容器(字符(的容器,它能解释为什么我在迭代元素时遇到分段错误吗?
是的,是的。
我没有时间从您的代码片段中创建一个自包含的示例,但这里有一些链接准确描述了您想要执行的操作:
- 标准::unordered_map 与 boost::共享内存中的进程间分配器 - 缺点?
- 提升进程间:在循环访问从结构引用对象的映射时删除字符串变量
相关文章:
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 是否可以使用分配器对象来释放另一个分配器分配的内存?
- C++17 和更新的 std::分配器是否适用于动态数量的自定义堆?
- 是否允许分配器构造和销毁成员函数从内部逻辑引发异常?
- shared_ptr的删除程序是否存储在自定义分配器分配的内存中?
- 包含字符串的结构的 Boost 进程间向量是否需要特殊的分配器?
- STD分配器是否会在堆上动态分配内存?它可以安全地删除内存吗?
- 供应商是否将 new 和 malloc 实现为小型对象分配器
- STD ::分配器是否处理C 17中的过度对准类型
- std::函数是否支持自定义分配器
- 当涉及分配器时,是否有类似于复制和交换习惯用法的东西
- 如何测试分配器是否使用std::allocate进行内存分配
- 是否可以编写一个自定义 STL 分配器,该分配器使用指向用户提供的分配函数的指针
- 是否有 BOOST 池固定大小的分配器
- 是否有自定义内存分配器设计模式不在其分配中存储元数据
- 在交换链中为每个渲染目标创建一个分配器是否有意义
- 传递给std::basic_string的分配器是否可以使其方法为虚拟的
- 'Tag' boost::p ool_分配器和 boost::fast_pool_allocator 中的模板参数是否支持控制基础池的实例?
- STL 中的自定义分配器是否仅分配实际数据
- std::vector::swap()和有状态分配器是否应使所有迭代器无效