[英]Why does std::allocator<>::deallocate() have a size_type parameter which is not used?
When using an std::allocator
, the deallocate
function requires a pointer
argument, and a size_type
argument ( std::allocator<>::deallocate(std::allocator<>::pointer p, std::allocator<>::size_type)
. However, the size_type is not used, and is not optional either. So why is it there? It really confuses me, as it should be optional, or even not there, because it is not used in the function. 当使用std::allocator
, deallocate
函数需要一个pointer
参数和一个size_type
参数( std::allocator<>::deallocate(std::allocator<>::pointer p, std::allocator<>::size_type)
。但是,size_type没有使用,也不是可选的。那么它为什么会出现?它真的让我感到困惑,因为它应该是可选的,甚至不是,因为它没有在函数中使用。
Edit : MSVC's implementation of allocator deallocate
编辑 :MSVC的allocator deallocate
实现
void deallocate(pointer _Ptr, size_type)
{ // deallocate object at _Ptr, ignore size
::operator delete(_Ptr);
}
Even if the standard allocator does not use the size of the memory block that is to be freed, other allocators might. 即使标准分配器不使用要释放的内存块的大小,其他分配器也可能。 Therefore, the argument has to be there such that all STL code that uses allocators can use different allocators in the same way. 因此,参数必须存在,使得使用分配器的所有STL代码可以以相同的方式使用不同的分配器。
The standard allocator does not need the size argument because it remembers the size of each allocated block. 标准分配器不需要size参数,因为它会记住每个已分配块的大小。 However, this adds quite a bit of overhead to each allocation. 但是,这会给每个分配增加相当多的开销。
If the user of the allocator knows how large each block of memory is (this is very often the case), then one can use a custom allocator which saves this overhead, and tell the deallocate
function about the size of the block instead. 如果分配器的用户知道每个内存块有多大(这种情况经常发生),那么可以使用自定义分配器来节省这种开销,并告诉deallocate
函数关于块的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.