STL Vector 是否使用“new”?并“删除”默认情况下内存分配?
我正在为应用程序开发一个插件,其中内存应该由应用程序分配并跟踪它。因此,内存句柄应该以缓冲区的形式从主机应用程序获取,然后将它们返回给应用程序。现在,我计划使用 STL Vectors,我想知道它内部使用什么样的内存分配。
它在内部使用“新建”和“删除”功能吗?如果是这样,我可以用自己的函数重载“new”和“delete”吗?或者我应该创建自己的模板分配器,这对我来说似乎是一项艰巨的工作,因为我在创建自定义模板方面没有那么丰富的经验。
欢迎任何建议/示例代码。可以像这样从应用程序获取内存句柄
void* bufferH = NULL;
bufferH = MemReg()->New_Mem_Handle(size_of_buffer);
MemReg()->Dispose_Mem_Handle(bufferH); //Dispose it
I am working on a plugin for an application, where the memory should be allocated by the Application and keep track of it. Hence, memory handles should be obtained from the host application in the form of buffers and later on give them back to the application. Now, I am planning on using STL Vectors and I am wondering what sort of memory allocation does it use internally.
Does it use 'new' and 'delete' functions internally? If so, can I just overload 'new' and 'delete' with my own functions? Or should I create my own template allocator which looks like a difficult job for me since I am not that experienced in creating custom templates.
Any suggestions/sample code are welcome. Memory handles can be obtained from the application like this
void* bufferH = NULL;
bufferH = MemReg()->New_Mem_Handle(size_of_buffer);
MemReg()->Dispose_Mem_Handle(bufferH); //Dispose it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
vector
默认使用std::allocator
,使用全局operator new需要std::allocator
(即: :operator new(size_t)
) 获取内存(20.4.1.1)。但是,不需要每次调用allocator::allocate
时都调用一次。所以,是的,如果您替换全局运算符 new,则矢量将使用它,尽管不一定以真正允许您的实现“有效”管理内存的方式。原则上,您想要使用的任何特殊技巧都可以通过
std::allocator
抓取 10MB 块中的内存并进行子分配来完全无关。如果您有一个特定的实现,您可以查看其
vector
的行为方式,如果您计划的分配策略本质上是特定于平台的,那么这可能就足够了。vector
usesstd::allocator
by default, andstd::allocator
is required to use global operator new (that is,::operator new(size_t)
) to obtain the memory (20.4.1.1). However, it isn't required to call it exactly once per call toallocator::allocate
.So yes, if you replace global operator new then
vector
will use it, although not necessarily in a way that really allows your implementation to manage memory "efficiently". Any special tricks you want to use could, in principle, be made completely irrelevant bystd::allocator
grabbing memory in 10MB chunks and sub-allocating.If you have a particular implementation in mind, you can look at how its
vector
behaves, which is probably good enough if your planned allocation strategy is inherently platform-specific.STL 容器使用在构造时给定的分配器,带有 默认分配器,使用
operator new
和operator delete
。如果您发现默认分配器不适合您,您可以提供符合容器要求的自定义分配器。 此处引用了一些现实世界的示例。
我会首先使用默认值来衡量性能,然后仅在确实需要时进行优化。分配器抽象为您提供了一种相对干净的方式来进行微调,而无需进行重大重新设计。如何使用
向量
可能比底层分配器(提前reserve()
,避免在元素范围中间插入和删除,处理有效地复制元素的构造 - 标准警告)。STL containers use an allocator they are given at construction time, with a default allocator that uses
operator new
andoperator delete
.If you find the default is not working for you, you can provide a custom allocator that conforms to the container's requirements. There are some real-world examples cited here.
I would measure performance using the default first, and optimize only if you really need to. The allocator abstraction offers you a relatively clean way to fine-tune here without major redesign. How you use the
vector
could have far more performance impact than the underlying allocator (reserve()
in advance, avoid insert and removal in the middle of the range of elements, handle copy construction of elements efficiently - the standard caveats).std::vector
使用unialized_*
函数从原始内存构造其元素(使用新的放置)。它使用创建时使用的任何分配器来分配存储,默认情况下,该分配器直接使用::operator new(size_t)
和::operator delete(void *p)
(即,不是特定于类型的operator new
)。std::vector
uses theunitialized_*
functions to construct its elements from raw memory (using placement new). It allocates storage using whatever allocator it was created with, and by default, that allocator uses::operator new(size_t)
and::operator delete(void *p)
directly (i.e., not a type specificoperator new
).从这篇文章中,“分配器的概念最初是引入该标准是为了为不同的内存模型提供抽象,以处理在某些 16 位操作系统上具有不同指针类型(例如近、远等)的问题”...
“该标准提供了一个分配器,该分配器在内部使用全局运算符“new”和“delete””
作者还指出分配器界面并不那么可怕。正如尼尔·布坎南所说,“你自己尝试一下!”
From this article, "The concept of allocators was originally introduced to provide an abstraction for different memory models to handle the problem of having different pointer types on certain 16-bit operating systems (such as near, far, and so forth)" ...
"The standard provides an allocator that internally uses the global operators 'new' and 'delete'"
The author also points out the alocator interface isn't that scary. As Neil Buchanan would say, "try it yourself!"
实际的 std::allocator 已针对相当大范围的大小对象进行了优化。当涉及到分配许多小对象时,它不是最好的,对于许多大对象来说,它也不是最好的。话虽这么说,它也不是为多线程应用程序编写的。
我可以建议,在尝试编写自己的分配器之前,如果您要使用多个分配器,请先检查 Hoard 分配器螺纹路线。 (或者您也可以查看同样有吸引力的英特尔 TBB 页面。)
The actual
std::allocator
has been optimized for a rather large extent of size objects. It isn't the best when it comes to allocating many small objects nor is it the best for many large objects. That being said, it also wasn't written for multi-threaded applications.May I suggest, before attempting to write your own you check out the Hoard allocator if you're going the multi-threaded route. (Or you can check out the equally appealing Intel TBB page too.)