简体   繁体   English

为什么std :: allocator <> :: deallocate()有一个未使用的size_type参数?

[英]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::allocatordeallocate函数需要一个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.

相关问题 为什么std :: allocator :: deallocate需要一个大小? - why does std::allocator::deallocate require a size? 为什么 std::span 缺少 size_type? - Why does std::span lack size_type? 为什么我不能使用QList :: size_type,因为我会使用std :: string :: size_type? (模板参数错误) - Why can't I use QList::size_type as I would std::string::size_type? (template parameter error) 为什么std :: allocator :: deallocate不是noexcept? - Why is std::allocator::deallocate not noexcept? 对于具有默认分配器的标准容器,std :: container :: size_type是否保证为size_t? - Is std::container::size_type guaranteed to be size_t for standard containers with default allocator? 为什么`std::set::erase(const key_type&amp;)`返回`size_type`而不是`bool`? - Why does `std::set::erase(const key_type&)` return `size_type` instead of `bool`? 为什么“size_type”变量的地址在 C++ 中用作“stoi()”的参数? - Why does the address of a "size_type" variable is used as an argument of "stoi()" in C++? 为什么C ++ 11中没有向量(size_type n,const Allocator&alloc)? - Why is there no vector(size_type n, const Allocator& alloc) in C++11? 为什么 text.size() 返回 std::size_t 而不是 std::string::size_type - Why text.size() returns std::size_t and not std::string::size_type 为什么 std::array size_t 和 std::vector 中的 size_type 通常是 size_t? - Why is size_type in std::array size_t and in std::vector usually size_t?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM