标准库Allocator的简易实现(二)
自己实现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的简易实现(二)的更多相关文章
- 标准库Allocator的使用(一)
上一篇我们提到了new运算符以及它的工作步骤,其实无非是把两项工作独立出来: 1.申请原始内存 2.执行构造函数 delete也涉及了两个工作: 1.执行析构函数 2.释放原始内存 其实标准库提供了另 ...
- 标准库Allocator(三)uninitialized_fill等函数的实现
前面我们使用了uninitialized_fill,来批量初始化某一段内存. 下面提供三个函数的实现代码,这三个代码的共同点是: 1.遇到错误,抛出异常 2.出现异常时,把之前构造的对象全部销毁 所以 ...
- c运行库、c标准库、windows API的区别和联系
C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的. API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...
- c运行时库,c标准库,Windows系统api的关系
原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...
- (转)c运行库、c标准库、windows API的区别和联系
C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的. API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...
- 怎么使用C++标准库来实现二维数组
在编程里,像界面布局是二维的,那么常常使用二维数组来表示界面的元素,那么就需要使用二维的数组,在现在C++肯定是以标准库为基础了,不再使用C的二维数组,那么怎么样做呢?下面就使用vector来实现二维 ...
- Python3标准库(二) re模块
正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...
- python 教程 第十二章、 标准库
第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1) sys模块 import sys if len(sys.argv) ...
- python练习 - 系统基本信息获取(sys标准库)+ 二维数据表格输出(tabulate库)
系统基本信息获取 描述 获取系统的递归深度.当前执行文件路径.系统最大UNICODE编码值等3个信息,并打印输出. ...
随机推荐
- HDU 4649 Professor Tian (概率DP)
Professor Tian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- 莫队 [洛谷2709] 小B的询问[洛谷1903]【模板】分块/带修改莫队(数颜色)
莫队--------一个优雅的暴力 莫队是一个可以在O(n√n)内求出绝大部分无修改的离线的区间问题的答案(只要问题满足转移是O(1)的)即你已知区间[l,r]的解,能在O(1)的时间内求出[l-1, ...
- cocos2d Programming Guide
http://python.cocos2d.org/doc/programming_guide/index.html The cocos2d Programming Guide provides in ...
- Android Sshd使用
1. 介绍 因为某些原因, 笔者需要在android上开发, 使用adb比较麻烦, 于是想使用sshd. 推荐的软件是openssh, 其他选择有dropbear, mosh.当然还有其他选择, 如 ...
- Javascript报错Converting circular structure to JSON 错误排解
在运行nodejs程序的时候报出以下的错误: 2017-11-20 17:44 +08:00: TypeError: Converting circular structure to JSON at ...
- DOS版PE工具制作
// PE.cpp : 定义控制台应用程序的入口点. //DOS版PE工具制作 #include "stdafx.h" #include <windows.h> #in ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- HDU 1203 【01背包/小数/概率DP】
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 51nod 1094 和为k的连续区间【前缀和/区间差/map】
1094 和为k的连续区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 一整数数列a1, a2, ... , an(有正有负),以及另一个整数k ...
- DP【p2051(bzoj 1801)】 [AHOI2009]中国象棋.
题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走方式是 ...