簡單說說STL的內(nèi)存管理
1. 概述
STL Allocator是STL的內(nèi)存管理器,也是最低調(diào)的部分之一,你可能使用了3年stl,但卻不知其為何物。
STL標(biāo)準(zhǔn)如下介紹Allocator
the STL includes some low-level mechanisms for allocating and deallocating memory. Allocators are very specialized, and you can safely ignore them for almost all purposes. Allocators encapsulate allocation and deallocation of memory. They provide a low-level interface that permits efficient allocation of many small objects; different allocator types represent different schemes for memory management.
<STL 源碼剖析>將其描述為空間配置器,理由是allocator可以將其它存儲介質(zhì)(例如硬盤)做為stl 容器的存儲空間。由于內(nèi)存是allocator管理的主要部分,因此,本文以STL內(nèi)存管理為出發(fā)點(diǎn)介紹allocator。
Allocator就在我們身邊,通常使用STL的方式:
#include <vector>
std::vector<int> Array(100);
本質(zhì)上,調(diào)用的是:
#include <vector>
std::vector<int, std::allocator> Array(100);
std::allocator就是一個(gè)簡單的Allocator
2. 使用
針對不同的應(yīng)用場合,STL中實(shí)現(xiàn)了不同的Allocator,如下(gcc-3.4:http://www.cs.huji.ac.il/~etsman/Docs/gcc-3.4-base/libstdc++/html/20_util/allocator.html):
__gnu_cxx::new_allocator<T> Simply wraps ::operator new and ::operator delete.
__gnu_cxx::malloc_allocator<T> Simply wraps malloc and free. There is also a hook for an out-of-memory handler
__gnu_cxx::debug_allocator<T> A wrapper around an arbitrary allocator A. It passes on slightly increased size requests to A, and uses the extra memory to store size information.
__gnu_cxx::__pool_alloc<bool, int> A high-performance, single pool allocator. The reusable memory is shared among identical instantiations of this type.
__gnu_cxx::__mt_alloc<T> A high-performance fixed-size allocatorthat was initially developed specifically to suit the needs of multi threaded applications
__gnu_cxx::bitmap_allocato A high-performance allocator that uses a bit-map to keep track of the used and unused memory locations
例如,在多線程環(huán)境下,可以使用:
#include <vector>
#include <mt_allocator.h>
std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100);
3.一個(gè)簡單的Allocator實(shí)現(xiàn)
我們可以實(shí)現(xiàn)自己的allocator
#include <memory>
template<class T>
class my_allocator : public std::allocator<T>
{
public:
typedef std::allocator<T> base_type;
// 必須要重新定義
template<class Other>
struct rebind
{
typedef my_allocator<Other> other;
};
// 內(nèi)存的分配與釋放可以實(shí)現(xiàn)為自定義的算法
pointer allocate(size_type count)
{
return (base_type::allocate(count));
}
void deallocate(pointer ptr, size_type count)
{
base_type::deallocate(ptr, count);
}
// 構(gòu)造函數(shù)
my_allocator()
{}
my_allocator(my_allocator<T> const&)
{}
my_allocator<T>& operator=(my_allocator<T> const&)
{
return (*this);
}
template<class Other>
my_allocator(my_allocator<Other> const&)
{}
template<class Other>
my_allocator<T>& operator=(my_allocator<Other> const&)
{
return (*this); }
};
相關(guān)文章
一文讓你不再害怕指針之C指針詳解(經(jīng)典,非常詳細(xì))
這篇文章主要給大家介紹了C指針的相關(guān)資料,文中介紹的很經(jīng)典,非常詳細(xì),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C指針具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08詳解C語言中index()函數(shù)和rindex()函數(shù)的用法
這篇文章主要介紹了C語言中index()函數(shù)和rndex()函數(shù)的用法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,要的朋友可以參考下2015-08-08Qt圖形圖像開發(fā)之高性能曲線圖模塊QCustomplot庫詳細(xì)使用方法與實(shí)例(支持動(dòng)、靜曲線圖)
這篇文章主要介紹了Qt圖形圖像開發(fā)之高性能曲線圖模塊QCustomplot庫詳細(xì)使用方法與實(shí)例(支持動(dòng)、靜曲線圖),需要的朋友可以參考下2020-03-03C++編程中將引用類型作為函數(shù)參數(shù)的方法指南
這篇文章主要介紹了C++編程中將引用類型作為函數(shù)參數(shù)的方法指南,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09C語言超詳細(xì)講解猜數(shù)字游戲的實(shí)現(xiàn)
現(xiàn)在很多游戲都有抽獎(jiǎng)抽卡的功能,其實(shí)這個(gè)就類似于猜數(shù)字,生成一個(gè)隨機(jī)數(shù),然后你去猜,猜對了就得獎(jiǎng)。猜到一定次數(shù)就會(huì)保底。要實(shí)現(xiàn)猜數(shù)字的小游戲,首先是要讓程序生成隨機(jī)數(shù),這就要用到rand、srand和time這三個(gè)函數(shù),其次要了解時(shí)間戳2022-07-07C++?Cartographer源碼中關(guān)于傳感器的數(shù)據(jù)傳遞實(shí)現(xiàn)
這篇文章主要介紹了C++?Cartographer源碼中關(guān)于傳感器的數(shù)據(jù)傳遞實(shí)現(xiàn),前面已經(jīng)談到了Cartographer中添加軌跡的方法和傳感器的數(shù)據(jù)流動(dòng)走向。發(fā)現(xiàn)在此調(diào)用了LaunchSubscribers這個(gè)函數(shù)來訂閱相關(guān)傳感器數(shù)據(jù)2023-03-03c++中#include <>與#include""的區(qū)別詳細(xì)解析
<>先去系統(tǒng)目錄中找頭文件,如果沒有在到當(dāng)前目錄下找。所以像標(biāo)準(zhǔn)的頭文件 stdio.h、stdlib.h等用這個(gè)方法2013-10-10