鍍金池/ 問(wèn)答/C++  Linux  網(wǎng)絡(luò)安全/ stl里面的容器里的allocator是如何能夠接管operator new的?

stl里面的容器里的allocator是如何能夠接管operator new的?

我們一般new一個(gè)對(duì)象,申請(qǐng)內(nèi)存的過(guò)程是operator new完成的,那么stl中的allocator是如何接管operator new完成內(nèi)存申請(qǐng)的工作呢?對(duì)象的內(nèi)存申請(qǐng)權(quán)是如何轉(zhuǎn)移的?我看stl里面也沒(méi)有重載operator new呀,另外如果在棧上生成我們的stl對(duì)象,也會(huì)經(jīng)過(guò)allocator嗎?

回答
編輯回答
六扇門

In the formal c++, 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.

stl中的allocator是如何接管operator new完成內(nèi)存申請(qǐng)的工作呢?對(duì)象的內(nèi)存申請(qǐng)權(quán)是如何轉(zhuǎn)移的?
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.

另外如果在棧上生成我們的stl對(duì)象,也會(huì)經(jīng)過(guò)allocator嗎?

There is no term stack in c++(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.

2017年1月6日 13:33