国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C > 正文

STL的內(nèi)存管理

2020-02-24 14:31:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

STL提供了很多泛型容器,很多程序員是使用這些容器的時(shí)候都只關(guān)心何時(shí)往容器內(nèi)塞對(duì)象,而不用關(guān)心如何管理內(nèi)存,其實(shí)管理內(nèi)存也是非常重要的,那么我們現(xiàn)在就去看看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可以將其它存儲(chǔ)介質(zhì)(例如硬盤)做為stl 容器的存儲(chǔ)空間。由于內(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è)簡(jiǎn)單的Allocator

2. 使用
針對(duì)不同的應(yīng)用場(chǎ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è)簡(jiǎn)單的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); }?
};??

內(nèi)存是allocator管理的主要部分,因此STL的內(nèi)存管理也是非常重要的哦,如果你想了解更深一點(diǎn)的可以進(jìn)入武林技術(shù)頻道進(jìn)行了解哦。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 东乡族自治县| 巴林左旗| 正安县| 兴义市| 濮阳市| 黑河市| 油尖旺区| 宜宾县| 乐都县| 故城县| 华安县| 棋牌| 津南区| 广灵县| 尼勒克县| 高碑店市| 个旧市| 达孜县| 福安市| 浙江省| 老河口市| 隆昌县| 平遥县| 海门市| 高台县| 兴化市| 定远县| 开平市| 民和| 桂林市| 甘谷县| 河西区| 克山县| 辽源市| 溧水县| 保亭| 会泽县| 辽宁省| 柘城县| 汶川县| 临泽县|