自己实现Allocator并不难,其实只需要改变allocate和deallocate,来实现自己的内存分配策略。

 

下面是一个std::allocator的模拟实现

#ifndef ALLOCATOR_HPP
#define ALLOCATOR_HPP #include <stddef.h>
#include <limits> template <typename T>
class Allocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type; //Allocator::rebind<T2>::other
template <typename V>
struct rebind
{
typedef Allocator<V> other;
}; pointer address(reference value) const { return &value; }
const_pointer address(const_reference value) const { return &value; } Allocator() throw() { }
Allocator(const Allocator &) throw() { }
//不同类型的allcator可以相互复制
template <typename V> Allocator(const Allocator<V> &other) { }
~Allocator() throw() { } //最多可以分配的数目
size_type max_size() const throw()
{ return std::numeric_limits<size_type>::max() / sizeof(T); } //分配内存,返回该类型的指针
pointer allocate(size_type num)
{ return (pointer)(::operator new(num * sizeof(T))); } //执行构造函数,构建一个对象
void construct(pointer p, const T &value)
{ new ((void*)p) T(value); } //销毁对象
void destroy(pointer p)
{ p->~T(); } //释放内存
void deallocate(pointer p, size_type num)
{ ::operator delete((void *)p); }
}; //这两个运算符不需要friend声明
template <typename T, typename V>
bool operator==(const Allocator<T> &, const Allocator<V> &) throw()
{ return true; } template <typename T, typename V>
bool operator!=(const Allocator<T> &, const Allocator<V> &) throw()
{ return false; } #endif

这里注意rebind的实现,如果需要使用Test的分配器分配其他类型,就可以这样:

Allocator<Test>::rebind<Test2>::other alloc;

测试代码如下:

#include "Allocator.hpp"
#include <string>
#include <vector>
using namespace std; int main(int argc, char const *argv[])
{
vector<string, Allocator<string> > vec(10, "haha"); vec.push_back("foo");
vec.push_back("bar"); //Allocator<Test>::rebind<Test2>::other alloc; return 0;
}

标准库Allocator的简易实现(二)的更多相关文章

  1. 标准库Allocator的使用(一)

    上一篇我们提到了new运算符以及它的工作步骤,其实无非是把两项工作独立出来: 1.申请原始内存 2.执行构造函数 delete也涉及了两个工作: 1.执行析构函数 2.释放原始内存 其实标准库提供了另 ...

  2. 标准库Allocator(三)uninitialized_fill等函数的实现

    前面我们使用了uninitialized_fill,来批量初始化某一段内存. 下面提供三个函数的实现代码,这三个代码的共同点是: 1.遇到错误,抛出异常 2.出现异常时,把之前构造的对象全部销毁 所以 ...

  3. c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  4. c运行时库,c标准库,Windows系统api的关系

    原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...

  5. (转)c运行库、c标准库、windows API的区别和联系

    C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的.  API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...

  6. 怎么使用C++标准库来实现二维数组

    在编程里,像界面布局是二维的,那么常常使用二维数组来表示界面的元素,那么就需要使用二维的数组,在现在C++肯定是以标准库为基础了,不再使用C的二维数组,那么怎么样做呢?下面就使用vector来实现二维 ...

  7. Python3标准库(二) re模块

    正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...

  8. python 教程 第十二章、 标准库

    第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1)    sys模块 import sys if len(sys.argv) ...

  9. python练习 - 系统基本信息获取(sys标准库)+ 二维数据表格输出(tabulate库)

    系统基本信息获取 描述 获取系统的递归深度.当前执行文件路径.系统最大UNICODE编码值等3个信息,并打印输出.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮ ...

随机推荐

  1. C语言.c和.h

    简单的说其实要理解C文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程,一般说来编译器会做以下几个过程:       1.预处理阶段 2.词法与语法分析阶段 3.编译阶段,首先编译成 ...

  2. [LeetCode] Combinations 回溯

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  3. l2tp连接不上,修复

    启动依赖服务并设置为开机自启动 保存为.bat文件管理员权限执行 ::显示名称IPsec Policy Agent sc config "PolicyAgent" start= a ...

  4. 【计算机网络】HTTP协议详解

    详见:http://blog.csdn.net/gueter/article/details/1524447 不让转载,但写得很好  

  5. 【转载】SQL server connection KeepAlive

    1.什么是SQL server TCP连接的keep Alive? 简单说,keep alive 是SQL server在建立每一个TCP 连接的时候,指定了TCP 协议的keepaliveinter ...

  6. OpenCV和Boost C++库的安装

    关于一般的安装步骤,此博客给出了详细的OpenCV的安装.一个步骤也不要落下,应该是不会出问题的. 主要的坑在Boost. 不知什么原因,我的电脑装boost_1_62_0-msvc-14.0-64, ...

  7. 洛谷——P2640 神秘磁石

    P2640 神秘磁石 题目背景 在遥远的阿拉德大陆,有一种神秘的磁石,是由魔皇制作出来的, 题目描述 1.若给他一个一维坐标系,那么他的磁力一定要在素数坐标的位置上才能发挥的最大(不管位置坐标的大小, ...

  8. 洛谷——P1614 爱与愁的心痛

    题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第一章 <我为歌狂>当中伍思凯神曲<舞月光>居然没赢给萨顶顶,爱与愁 ...

  9. IE8兼容性问题

    由于业务的需要,我们竟然还要支持IE8,听着就让人很心酸呀.不过在进行适配的过程中,会发现还是有一定规律的,基本上帮相关问题改了,页面也就能正常显示了.下面就总结下对IE8适配过程中所进行的修改. 1 ...

  10. ffmpeg yasm not found, use --disable-yasm for a crippled build

    yasm是汇编编译器,因为ffmpeg中为了提高效率用到了汇编指令,比如MMX和SSE.解决这个问题方面有两个: 1.在网上下载一个yasm.exe并安装在mingw/bin下面,编译代码时你注意看, ...