#include <iostream>
#include <new>
#include <cstddef>
#include <vector>
using namespace std;
template <class T>
inline T* _allocator(ptrdiff_t size, T*)
{
std::cout<<"_allocator()"<<std::endl;
set_new_handler(0);
T* tmp = (T*)(::operator new((size_t)size * sizeof(T)));
if (tmp == NULL)
{
exit(1);
}
return tmp;
} template<class T>
inline void _deallocator(T * buffer)
{
std::cout<<"_deallocator()"<<std::endl;
::operator delete(buffer);
} template<class T1, class T2>
inline void _constructor(T1* p, const T2& value)
{
std::cout<<"_constructor()"<<std::endl;
new(p) T1(value);
} template <class T>
inline void _destroy(T* ptr)
{
std::cout<<"_destroy()"<<std::endl;
ptr->~T();
} template<class T>
class jjAllocator
{
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template<class U>
struct rebind
{
typedef jjAllocator<U> other;
}; jjAllocator() {}
jjAllocator(const jjAllocator&) {} template <class U>
jjAllocator(const jjAllocator<U>&) {} ~jjAllocator() {} pointer allocate(size_type n, const void* hint = 0)
{
return _allocator((ptrdiff_t)n, (pointer)0);
} void deallocate(pointer p, size_type n)
{
_deallocator(p);
} void construct(pointer p, const T& value)
{
_constructor(p, value);
} void destroy(pointer p)
{
_destroy(p);
} pointer address(reference x)
{
return (pointer)&x;
} const_pointer const_address(const_reference x)
{
return (const_pointer)&x;
} size_type max_size()const
{
return size_type( UINT_MAX/sizeof(T));
}
}; int main()
{
std::vector<int, jjAllocator<int>>mmm;
mmm.push_back(1);
mmm.push_back(2);
system("pause");
return 1;
}

STL源码剖析01-allocator的更多相关文章

  1. STL源码剖析 — 空间配置器(allocator)

    前言 以STL的实现角度而言,第一个需要介绍的就是空间配置器,因为整个STL的操作对象都存放在容器之中. 你完全可以实现一个直接向硬件存取空间的allocator. 下面介绍的是SGI STL提供的配 ...

  2. (原创滴~)STL源码剖析读书总结1——GP和内存管理

    读完侯捷先生的<STL源码剖析>,感觉真如他本人所说的"庖丁解牛,恢恢乎游刃有余",STL底层的实现一览无余,给人一种自己的C++水平又提升了一个level的幻觉,呵呵 ...

  3. 《STL源码剖析》相关面试题总结

    原文链接:http://www.cnblogs.com/raichen/p/5817158.html 一.STL简介 STL提供六大组件,彼此可以组合套用: 容器容器就是各种数据结构,我就不多说,看看 ...

  4. STL源码剖析之序列式容器

    最近由于找工作需要,准备深入学习一下STL源码,我看的是侯捷所著的<STL源码剖析>.之所以看这本书主要是由于我过去曾经接触过一些台湾人,我一直觉得台湾人非常不错(这里不涉及任何政治,仅限 ...

  5. stl源码分析之allocator

    allocator封装了stl标准程序库的内存管理系统,标准库的string,容器,算法和部分iostream都是通过allocator分配和释放内存的.标准库的组件有一个参数指定使用的allocat ...

  6. 《STL源码剖析》读书笔记

    转载:https://www.cnblogs.com/xiaoyi115/p/3721922.html 直接逼入正题. Standard Template Library简称STL.STL可分为容器( ...

  7. 面试题总结(三)、《STL源码剖析》相关面试题总结

    声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...

  8. 通读《STL源码剖析》之后的一点读书笔记

    直接逼入正题. Standard Template Library简称STL.STL可分为容器(containers).迭代器(iterators).空间配置器(allocator).配接器(adap ...

  9. 【STL 源码剖析】浅谈 STL 迭代器与 traits 编程技法

    大家好,我是小贺. 点赞再看,养成习惯 文章每周持续更新,可以微信搜索「herongwei」第一时间阅读和催更,本文 GitHub : https://github.com/rongweihe/Mor ...

  10. STL"源码"剖析-重点知识总结

    STL是C++重要的组件之一,大学时看过<STL源码剖析>这本书,这几天复习了一下,总结出以下LZ认为比较重要的知识点,内容有点略多 :) 1.STL概述 STL提供六大组件,彼此可以组合 ...

随机推荐

  1. php 实现简拼

    <blockquote>model::::::::::::::::::::::::::::: function getFirstCharter($str){if(empty($str)){ ...

  2. angular和vue还有jquery的区别

    angularjs简单介绍和特点 首先angular是一个mvc框架, 使用mvc解耦, 采用model, controller以及view的方式去组织代码, 会将一个html页面分成若干个模块, 每 ...

  3. Python3 tkinter基础 Menu add_cascade 多级菜单 add_separator 分割线

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. Git的初次使用

    一.配置本地Git库 1.下载安装好Git,并配置自己的信息. git config --global user.name"yourname"配置你的名称 git config - ...

  5. 面试必问的SpringCloud实现原理图

    引言 面试中面试官喜欢问组件的实现原理,尤其是常用技术,我们平时使用了SpringCloud还需要了解它的实现原理,这样不仅起到举一反三的作用,还能帮助轻松应对各种问题及有针对的进行扩展. 以下是 课 ...

  6. 全局解释器锁GIL & 线程锁

    1.GIL锁(Global Interpreter Lock) Python代码的执行由Python虚拟机(也叫解释器主循环)来控制.Python在设计之初就考虑到要在主循环中,同时只有一个线程在执行 ...

  7. spring-cloud-config-server——Environment Repository(Git Backend)

    参考资料: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.4.0.RELEASE/single/spring-cl ...

  8. Maven提示找不到dependency依赖包

    最近C盘爆炸,又重装了一下系统,打开之前创建的Maven项目,提示找不到仓库里的jar包了 我更新了Maven,重新加载都没用,我去系统盘查看的时候发现了这么一个情况: 我在装WIN10的时候用的是U ...

  9. 解决Firefox显示“已阻止载入混合活动内容”的方法

    今天把项目放到服务器上了,调试的时候出现“已阻止载入混合活动内容……”的报错: 解决方法如下: 方法1:让Firefox暂时不阻止 打开新标签页,在地址栏输入 about:config,进入配置页面. ...

  10. R apply() 函数和 tapply() 函数

    apply(a,b,c) a是矩阵 b是行或列的代表,1是行,2是列 c是执行函数,如求和-sum,求平均-mean,求-range tapply(a,b,c) a是一个一维数据,           ...