简单说说STL的内存管理
1. 概述
STL Allocator是STL的内存管理器,也是最低调的部分之一,你可能使用了3年stl,但却不知其为何物。
STL标准如下介绍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可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator。
Allocator就在我们身边,通常使用STL的方式:
#include <vector>
std::vector<int> Array(100);
本质上,调用的是:
#include <vector>
std::vector<int, std::allocator> Array(100);
std::allocator就是一个简单的Allocator
2. 使用
针对不同的应用场合,STL中实现了不同的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
例如,在多线程环境下,可以使用:
#include <vector>
#include <mt_allocator.h>
std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100);
3.一个简单的Allocator实现
我们可以实现自己的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;
};
// 内存的分配与释放可以实现为自定义的算法
pointer allocate(size_type count)
{
return (base_type::allocate(count));
}
void deallocate(pointer ptr, size_type count)
{
base_type::deallocate(ptr, count);
}
// 构造函数
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); }
};
相关文章
- vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
- 本篇文章是对C#中内存管理以及优化的方法进行了详细的分析介绍,需要的朋友参考下...2020-06-25
- 这篇文章主要介绍了zlib库压缩和解压字符串STL string的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下...2020-04-25
- 这篇文章主要介绍了C++ STL中的容器适配器实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-20
- <STL 源码剖析>将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator...2020-04-25
stl常用算法(Algorithms)介绍(stl排序算法、非变序型队列)
这篇文章主要介绍了stl常用算法(Algorithms)介绍(stl排序算法、非变序型队列),需要的朋友可以参考下...2020-04-25- 下面小编就为大家带来一篇老生常谈C/C++内存管理。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
stl容器set,map,vector之erase用法与返回值详细解析
在使用 list、set 或 map遍历删除某些元素时可以这样使用,如下所示...2020-04-25- 关于set,必须说明的是set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序...2020-04-25
- 这篇文章主要给大家分享Net内存管理五大基础内容,文章讲围绕Net内存管理详细介绍文章内容,感兴趣的朋友可以参考一下,希望对你有所帮助...2021-10-19
- 这篇文章主要介绍了详解MySQL InnoDB存储引擎的内存管理,帮助大家更好的理解和学习使用MySQL数据库,感兴趣的朋友可以了解下...2021-04-08
- 这篇文章主要介绍了C++在成员函数中使用STL的find_if函数实例,包括了STL中find_if函数的具体用法及相关的完整实例,非常具有参考借鉴价值,需要的朋友可以参考下...2020-04-25
- "养成良好的编程习惯"其实是相当综合的一个命题,可以从多个角度、维度和层次进行论述和评判。如代码的风格、效率和可读性;模块设计的灵活性、可扩展性和耦合度等等。要试图把所有方面都阐述清楚必须花很多的精力,而且也不一定能阐述得全面。因此,本系列文章以软件开发的基础问题为切入点,阐述程序设计和代码编写方面的细节问题,以点带面,旨在激发大家的思考与总结,希望能为大家带来实际的帮助...2020-04-25
Spring MVC使用jstl 标签c:forEach 遍历输出双层嵌套List的数据方式
这篇文章主要介绍了Spring MVC使用jstl 标签c:forEach 遍历输出双层嵌套List的数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-16c++利用stl set_difference对车辆进出区域进行判定
这篇文章主要介绍了set_difference,用于求两个集合的差集,结果集合中包含所有属于第一个集合但不属于第二个集合的元素,需要的朋友可以参考下...2020-04-25C++ STL入门教程(2) list双向链表使用方法(附程序代码)
这篇文章主要为大家详细介绍了C++ STL入门教程第二篇,list双向链表使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25- 这篇文章是关于C++语言的一个新的扩展——标准模板库的(Standard Template Library),也叫STL...2020-04-25
- 这篇文章主要为大家介绍了C++的内存管理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助...2021-11-18
- 这篇文章主要给大家介绍了关于vector的特殊性之为什么它不是STL容器的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c++具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-04-25
- 本篇文章对c++中STL容器中的vertor与list的应用进行了详细的分析解释。需要的朋友参考下...2020-04-25