第十一节 分配器
- STL源码学习----内存管理
- 分配器的好坏影响到容器的性能
operator new()
里面调用malloc
D:Program Files (x86)Microsoft Visual Studio 12.0VCcrtsrc
vc下的源代码路径
- 分配的内存要比申请的内存大
- 所以VC6.0下的allocator没有优化
// newop operator new(size_t) for Microsoft C++
#include <cstdlib>
#include <new>
#include <xutility>
_C_LIB_DECL
int __cdecl _callnewh(size_t count) _THROW1(_STD bad_alloc);
_END_C_LIB_DECL
void *__CRTDECL operator new(size_t count) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(count)) == 0)
if (_callnewh(count) == 0)
{ // report no memory
_STD _Xbad_alloc();
}
return (p);
}
/*
* Copyright (c) 1992-2007 by P.J. Plauger. ALL RIGHTS RESERVED.
* Consult your license regarding permissions and restrictions.
V5.03:0009 */
allocator
主要的两个操作是:allicate
和deallocate
;其里面调用operator new
和operator delete
。
- 没必要自己手动去写
int *p=allocator<int>().allocate(512,(int*)0)
;和allocator<int>().deallocate(p,512)
。要写明释放内存的大小,所以很难用,但是容器不需要考虑这些。
- VC版本如下:

- BC版本:

- Gnu 2.9版本:

- 不太理想,额外开销大,包装一层后额外开销大;
- SGI STL不使用这个allocator
- STL使用
alloc
:

- 16条链表,每条链表下面申请空间,额外的空间是Cookie,记录内存的大小

- Gnu 4.9版下:
- 使用的allocator,没有使用alloc;为什莫不用,不知道!

- G4.9所附的标准库中,有许多extention allocators,其中
_pool_alloc
就是G2.9的alloc
.

第十二节 容器之间的实现关系与分类
- 依缩进的方式表示复合关系,有一个关系;比如:
priority_queue
里面有一个heap
支撑。
- 旁边为指针大小
