基于堆栈缓冲区的STL分配器? [英] Stack-buffer based STL allocator?

查看:68
本文介绍了基于堆栈缓冲区的STL分配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个符合C ++标准库的分配器是否使用驻留在堆栈上的(固定大小的)缓冲区是可行的。

I was wondering if it practicable to have an C++ standard library compliant allocator that uses a (fixed sized) buffer that lives on the stack.

以某种方式,尽管在其他地方隐式回答了 ,但似乎还没有这样问这个问题。

Somehow, it seems this question has not been ask this way yet on SO, although it may have been implicitly answered elsewhere.

基本上,就我的搜索而言,似乎应该可以创建使用固定大小缓冲区的分配器。现在,乍看之下,这应该意味着 也应该有一个分配器,该分配器使用固定大小的缓冲区,该缓冲区在栈中存活,但它确实出现

So basically, it seems, as far as my searches go, that it should be possible to create an allocator that uses a fixed size buffer. Now, on first glance, this should mean that it should also be possible to have an allocator that uses a fixed size buffer that "lives" on the stack, but it does appear, that there is no widespread such implementation around.

让我举一个例子说明我的意思:

Let me give an example of what I mean:

{ ...
  char buf[512];
  typedef ...hmm?... local_allocator; // should use buf
  typedef std::basic_string<char, std::char_traits<char>, local_allocator> lstring;
  lstring str; // string object of max. 512 char
}

这将如何实现?

answer这个问题(感谢R. Martinho Fernandes)从铬资源链接到基于堆栈的分配器: http://src.chromium.org/viewvc/chrome/trunk/src/base/stack_container.h

The answer to this other question (thanks to R. Martinho Fernandes) links to a stack based allocator from the chromium sources: http://src.chromium.org/viewvc/chrome/trunk/src/base/stack_container.h

但是,该类似乎非常特殊,尤其是因为此 StackAllocator 没有默认的ctor 时,我在想每个分配器类都需要一个默认的ctor

However, this class seems extremely peculiar, especially since this StackAllocator does not have a default ctor -- and there I was thinking that every allocator class needs a default ctor.

推荐答案

显然,在是符合从一个 Howard Hinnant 中访问堆栈分配器。

Apparently, there is a conforming Stack Allocator from one Howard Hinnant.

通过使用固定大小的缓冲区(通过引用的竞技场对象)并在请求过多空间时退回到堆中来工作。

It works by using a fixed size buffer (via a referenced arena object) and falling back to the heap if too much space is requested.

此分配器没有默认的ctor,并且由于霍华德说:

This allocator doesn't have a default ctor, and since Howard says:


我已经更新了这篇文章提供了一个完全符合C ++ 11的新分配器。

I've updated this article with a new allocator that is fully C++11 conforming.

我会说这不是一个必需的。分配器具有默认的ctor。

I'd say that it is not a requirement for an allocator to have a default ctor.

这篇关于基于堆栈缓冲区的STL分配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆