C++ std::allocator 超出范围时会发生什么?

C++ What happens when std::allocator goes out of scope?

当 std::allocator 超出范围时会发生什么?它管理的资源会发生什么变化?如果没有提前释放,是否泄露?

std::allocator 超出范围时,它管理的资源不会发生任何变化。

通过 std::allocator::allocate 分配的任何资源都应通过调用 std::allocator::deallocate.

显式释放

std::allocator的任何实例都可以用于释放,不一定是用于分配的实例,因为

The default allocator is stateless, that is, all instances of the given allocator are interchangeable, compare equal and can deallocate memory allocated by any other instance of the same allocator type.

(https://en.cppreference.com/w/cpp/memory/allocator)