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

发布于 2022-09-07 08:14:48 字数 161 浏览 26 评论 0

我们一般new一个对象,申请内存的过程是operator new完成的,那么stl中的allocator是如何接管operator new完成内存申请的工作呢?对象的内存申请权是如何转移的?我看stl里面也没有重载operator new呀,另外如果在栈上生成我们的stl对象,也会经过allocator吗?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2022-09-14 08:14:48

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完成内存申请的工作呢?对象的内存申请权是如何转移的?
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对象,也会经过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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文