为什么 std::allocator::deallocate 需要一个大小? [英] why does std::allocator::deallocate require a size?
问题描述
std::allocator
是对底层内存模型的抽象,它封装了调用new
和delete
的功能.delete
虽然不需要大小,但是 deallocate() 需要它.
std::allocator
is an abstraction over the underlying memory model, which wraps the functionality of calling new
and delete
. delete
doesn't need a size though, but deallocate() requires it.
void deallocate( T* p, std::size_t n );
参数 n 必须等于调用的第一个参数最初产生 p 的分配();否则,行为是未定义."
void deallocate( T* p, std::size_t n );
"The argument n must be equal to the first argument of the call to allocate() that originally produced p; otherwise, the behavior is undefined."
为什么?
现在我要么必须在释放之前进行额外的计算,要么开始存储我传递给分配的大小.如果我不使用分配器,我就不必这样做.
Now I either have to make additional calculations before deallocating, or start storing the sizes that I passed to allocate. If I didn't use the allocator I wouldn't have to do this.
推荐答案
std::allocator
API 的设计 - Allocator
概念 - 促进潜在替代者的工作.
The design of the std::allocator
API - the Allocator
concept - is to facilitate the job of potential replacements.
std::allocator
是对底层内存模型的抽象
std::allocator
is an abstraction over the underlying memory model
不必如此!通常,分配器不需要使用 C malloc
和 free
,也不需要 delete
或 not-in-place new代码>.是的,默认 通常会这样做,但分配器机制不仅仅是对 C 内存模型的抽象.与众不同通常是自定义分配器的全部目的.请记住,分配器是可替换的:特定的
std::allocator
可能不需要释放分配的大小,但任何替换都可能需要.
It doesn't have to be! In general, an allocator does not need to use C malloc
and free
, nor delete
or the not-in-place new
. Yes, the default one usually does it, but the allocator mechanism isn't merely an abstraction over C's memory model. To be different is often the whole purpose of a custom allocator. Remember that allocators are replaceable: a particular std::allocator
might not need the size for deallocation, but any replacements are likely to.
std::allocator
的兼容实现可以自由地断言您确实将正确的 n
传递给 deallocate
,否则依赖尺寸正确.
A compliant implementation of std::allocator
is free to assert that you indeed pass the correct n
to deallocate
, and to otherwise depend on the size being correct.
碰巧malloc
和free
将块大小存储在其数据结构中.但一般来说,分配器可能不会这样做,要求它这样做是过早的悲观.假设您有一个自定义池分配器并且正在分配 int
的块.在典型的 64 位系统上,存储 64 位 size_t
和 32 位 int
的开销为 200%.分配器的用户可以更好地将大小存储在分配中,或者以更便宜的方式确定大小.
It happens that malloc
and free
store the chunk size in its data structures. But in general an allocator might not do it, and requiring it to do so is premature pessimization. Suppose you had a custom pool allocator and were allocating chunks of int
s. On a typical 64-bit system it'd be a 200% overhead to store a 64-bit size_t
along with the 32-bit int
. The user of the allocator is much better positioned to either store the size along in the allocation, or to determine the size in a cheaper fashion.
好的 malloc 实现不会为每个小分配存储分配大小;它们并且能够从指针本身导出块大小,例如通过从块指针导出块指针,然后检查块头的块大小.这当然只是一个细节.您可以使用特定于平台的 API 获取大小的下限,例如 malloc_size
在 OS X 上,_msize
在 Windows 上,malloc_usable_size
在 Linux 上.
Good malloc implementations don't store allocation size for every small allocation; they and are able to derive the chunk size from the pointer itself e.g. by deriving a block pointer from the chunk pointer, and then inspecting the block header for the chunk size. That's but a detail of course. You could obtain the lower bound on the size using platform-specific APIs, such as malloc_size
on OS X, _msize
on Windows, malloc_usable_size
on Linux.
这篇关于为什么 std::allocator::deallocate 需要一个大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!