How can allocator in the container in stl take over operator new?

We usually new an object, and the process of requesting memory is completed by operator new, so how does allocator in stl take over operator new to complete the memory request? How is the memory request right of an object transferred? I don"t think operator new is overloaded in stl. In addition, if our stl object is generated on the stack, will it also pass through allocator?

Mar.12,2021

In the formal cPP, there is no such term called STL , STL is never an abbreviation of Standard Library or Standard Template Library . You can also refer to another my answer here. Yes, Allocators were invented by Alexander Stepanov. But his original library has been out of date and some components are adopted by the standard library.

how does allocator in stl take over operator new to do the memory request? How is the object's memory request right transferred?
all

From n4714 23.10.10.1 allocator members

[[nodiscard]] T* allocate (size_t n);
3 Remarks: the storage is obtained by calling :: operator new (21.6.2), but it is unspecified when or how often this function is called.
< hr >
in addition, if our stl object is generated on the stack, will it also pass through allocator?

There is no term stack in cPP (only stack unwinding, which is not relevant here), in c, there is a term called activition record . Even if speaking to implementation, the choice of call stack or register should be decided by calling convention . No matter call stack or register, it will not invoke oeprator new function.

Menu