欢迎来到天天文库
浏览记录
ID:21248068
大小:2.89 MB
页数:43页
时间:2018-10-20
《research of list-new》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、STLresearch–listSGI2.91Briefintroductionoflistmemoryallocate–allocatorTheinterfacesofallocatorTwotypesofallocatorsDatastructureoflist–BidirectionalcircularlinkedlistAlgorithmsandmembermethodsoflistBriefintroductionoflistlistisakindofsequencecontainer.Itisbidirectionalci
2、rcularlinkedlist,whichcanstoretheelementslocatedindiscontinuousstoragelocations.Advantages:Constanttimetoinsertordeleteanelementanywhereinthecontainer.Providingmoreefficientmemberfunctionthancommonalgorithmswithsamename,suchasremove()andunique().Theiteratorsoflistwillnot
3、beinvalidafterinsertionorremovalexcepttheonejustdeleted.Disadvantages:Lackdirectaccesstotheelementbyitsposition.memoryallocate-allocatorTheSTLcontainershavethefeatureofchangingtheirsizesduringtheruntime.Toachievethis,someformofdynamicmemoryallocationisrequired.Theimporta
4、ntinterfacesofalloctor:templateclassallocator{_Tp*allocate(size_type__n,constvoid*=0){return__n!=0?static_cast<_Tp*>(_Alloc::allocate(__n*sizeof(_Tp))):0;}voiddeallocate(pointer__p,size_type__n){_Alloc::deallocate(__p,__n*sizeof(_Tp));}voidconstruct(pointer__p,
5、const_Tp&__val){new(__p)_Tp(__val);}voiddestroy(pointer__p){__p->~_Tp();}}TwotypeofallocatorsTherearetwotypesofdefaultallocatorsusedbySTL:Firstlevelallocator:Handlethememoryrequestedthatismorethan128bytes.Secondlevelallocator:Handlethememoryrequestedwhichislessthan128byt
6、es.It’sdesignedtoavoidmemoryfragmentation.STLusesthesecondlevelallocatorindefaultunlessthememoryrequestedismorethan128bytes.Ifthemacroname“__USE_MALLOC”isdefined,itwillcallthefirstlevelallocator.memoryallocate–allocatorThefirstlevelallocatorThesecondlevelallocatorNexttem
7、plateclass__malloc_alloc_template{private://oom:outofmemorystaticvoid*_S_oom_malloc(size_t);staticvoid*_S_oom_realloc(void*,size_t);staticvoid(*__malloc_alloc_oom_handler)();public:staticvoid*allocate(size_t__n){void*__result=malloc(__n);if(0==__result)__resul
8、t=_S_oom_malloc(__n);return__result;}staticvoiddeallocate(void*__p,size_t/*__n*/){free(__p);}staticvoid
此文档下载收益归作者所有