阅读《STL源码分析》第二章 仿照书上2.1.1节编写了一个自己的简易空间配置器
1 储备知识
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t
ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.
size_type是unsigned类型,表示容器中元素长度或者下标,vector::size_type i = 0;
difference_type是signed类型,表示迭代器差距,vector:: difference_type = iter1-iter2.
当operator new申请一个内存失败的时候,它会进行如下的处理步骤:
1、如果存在客户指定的处理函数,则调用处理函数(new_handler),如果不存在则抛出一个异常。
2、继续申请内存分配请求。
3、判断申请内存是否成功,如果成功则返回内存指针,如果失败转向处理步骤1
为了自定义这个“用以处理内存不足”的函数new_handler,用户可以调用set_new_handler进行设置
这两个函数声明如下:
namespace std{
typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();
}
其中,new_handler是个typedef,定义一个函数指针,该函数没有参数,也没有返回值;
set_new_handler用于设置处理函数,设置p为当前处理函数,并返回之前的new_handler
当参数p为0时,即抛出异常
Q:set_new_handler(0)什么意思?
A:见知乎高赞回答:set_new_handler(0)什么意思?
但我的代码里放置这句话会报错,还是没懂
代码 环境VS2015
#ifndef _ZYTALLOC_
#define _ZYTALLOC_
//#include<new>
#include<cstddef>
#include<cstdlib>
#include<climits>
#include<iostream>
#include<memory>
#include<vector>
namespace ZYT
{
template<class T>
inline T* _allocate(ptrdiff_t size, T*)
{
//set_new_handler(0);
T*tmp = (T*)(::operator new((size_t)(size*sizeof(T))));
if (tmp == 0)
{
std::cout << "out of memeory" <<std:: endl;
exit(1);
}
return tmp;
}
template<class T>
inline void _deallocate(T* buffer)
{
::operator delete(buffer);
}
template<class T1,class T2>
inline void _construct(T1*p, const T2&value)
{
new(p) T1(value);
}
template<class T>
inline void _destroy(T*ptr)
{
ptr->~T();
}
template<class T>
class allocater {
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 allocater<U>other;
};
pointer allocate(size_type n, const void*hint = 0)
{
return _allocate<T>((difference_type)n, (pointer)0);
}
void deallocate(pointer p, size_type n) {
_deallocate(p); }
void construst(pointer p, const T&value) {
_construct(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)); }
};
}
#endif _ZYTALLOC_
int main()
{
using namespace ZYT;
ZYT::allocater<int>a;
int *ptr = a.allocate(2);
a.construst(ptr, 10);
a.construst(ptr + 1, 20);
std::cout << *ptr << std::endl;
std::cout << ptr[1] << std::endl;
a.destroy(ptr);
a.destroy(ptr + 1);
a.deallocate(ptr,2);
std::cout << *ptr << std::endl;
allocater<int>::rebind<double> ::other dalloc;
double *p = dalloc.allocate(2);
dalloc.construst(p, 3.5);
std::cout << *p << std::endl;
dalloc.destroy(p);
dalloc.deallocate(p, 2);
std::cout << *p << std::endl;
return 0;
}