简体   繁体   English

几个小的std :: vector的连续内存分配?

[英]Contiguous memory allocation for several small std::vectors?

I would like to find a way to store several std::vectors , each of a different but known and reasonably small size, in contiguous memory. 我想找到一种在连续内存中存储几个std::vectors ,每个std::vectors大小不同但已知且相当小。 I realize I could write my own class, say with a very large array and with pointers to the start of each subsection of the array within the larger array treated like a separate entity, but it seems like there should be a smarter way to do this. 我意识到我可以编写自己的类,例如使用一个非常大的数组,并使用指向较大数组中数组每个子节的指针的指针,将其视为一个单独的实体,但是似乎应该有一种更聪明的方法来做到这一点。

Is there a way to use allocators , for example, to create contiguous std::vectors ? 有没有一种使用allocators ,例如,创建连续的std::vectors I'd like not to reinvent the wheel just because I want this memory-locality of otherwise normal std::vectors 我不想仅仅因为我想要正常std::vectors这种内存位置而重新发明轮子

I don't know how to even begin coding. 我什至不知道如何开始编码。 I need to create an allocator that takes a pointer to memory, allocates a vector there, and then somehow passes back the address of the end of that vector, so the next std::vector 's allocator could grab that and do it again. 我需要创建一个分配器,该分配器使用一个指向内存的指针,在其中分配一个向量,然后以某种方式传递回该向量末尾的地址,以便下一个std::vector的分配器可以抓取并再次执行。 How can an allocator return a value? allocator如何返回值?

The solution is @HowardHinnant's short_alloc . 解决方法是@HowardHinnant的short_alloc I want to allocate on the heap so have to use new ,*** but otherwise Howard's posted code does exactly what I want. 我想在堆上分配,所以必须使用new ,***,否则霍华德发布的代码正是我想要的。

template <std::size_t N>
class arena
{...
char* buf_ = new char[N] 
// still need to align this but not sure of the syntax 
// to do that with a new statement
...

The missing piece from my perspective when I asked the question was that allocators can have constructors that take arguments: 当我问这个问题时,从我的角度来看,缺失的部分是allocators可以使constructors接受参数:

constexpr int N = 1000*sizeof(int);
arena<N> myArena;
std::vector<int, short_alloc<int, N>> x(MyArena);

I found the code reference in another SO post: Questions about Hinnant's stack allocator which was referenced from the CodeReview post Chris Drew suggested in his comment above. 我在另一篇SO帖子中找到了代码参考:Chris Drew在上面的评论中建议的CodeReview帖子中引用了有关Hinnant的堆栈分配器的问题 Thank you all. 谢谢你们。

*** The code does use new in the allocate method, leaving me unsure of whether this is allocated on the stack (as it appears from the declaration of buf_*) or on the heap (use of new )... ***代码的确在allocate方法中使用了new ,因此我不确定这是分配在堆栈上(从buf_ *的声明中出现)还是在堆上(使用new )...

For your requirement, I would implement custom allocator that extends std::allocator and overrides allocate, deallocate method that grabs chunks from a memory pool. 根据您的需求,我将实现自定义分配器,该自定义分配器扩展了std :: allocator并覆盖了从内存池中获取块的分配,释放方法。 If you already know the maximum size required, selecting memory pool size shouldn't be a problem. 如果您已经知道所需的最大大小,那么选择内存池大小应该不是问题。

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

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