容器空间配置器的重要性
以vector容器为例,vector的定义如下:
#include <iostream>
using namespace std;
template<typename T>
class vector
{
public:
vector(int size = 10)
{
_first = new T[size];
_last = _first;
_end = _first + size;
}
~vector()
{
delete[] _first;
_first = nullptr;
_last = nullptr;
_end = nullptr;
}
vector(const vector<T>& vec)
{
int size = vec._end - vec._first;
int len = vec._last - vec._first;
_first = new T[size];
for (int i = 0; i < len; i++)
{
_first[i] = vec._first[i];
}
_last = _first + len;;
_end = _first + size;
}
vector<T>& operator=(const vector<T>& vec)
{
if (this == &vec)
return *this;
delete[] _first;
int size = vec._end - vec._first;
int len = vec._last - vec._first;
_first = new T[size];
for (int i = 0; i < len; i++)
{
_first[i] = vec._first[i];
}
_last = _first + len;;
_end = _first + size;
return *this;
}
void push_back(const T& val)
{
if (isFull())
expend();
*_last++ = val;
}
void pop_back()
{
if (isEmpty())
return;
_last--;
}
T back()const
{
return *(_last - 1);
}
bool isEmpty()const { return _last == _first; }
bool isFull()const { return _end == _last; }
int size()const { return _last - _first; }
private:
void expend()
{
int len = _end - _first;
T* tmp = new T[len * 2];
for (int i = 0; i < len; i++)
{
tmp[i] = _first[i];
}
delete[]_first;
_first = tmp;
_last = _first + len;
_end = _first + len * 2;
}
private:
T* _first; //数组起始地址
T* _last; //最后一个有效元素的后继位置
T* _end; //数组空间的后继位置
};
int main()
{
Test t1, t2, t3;
cout << "============================" << endl;
vector<Test> vec;
vec.push_back(t1);
vec.push_back(t2);
vec.push_back(t3);
cout << "============================" << endl;
vec.pop_back();
cout << "============================" << endl;
}
如果容器内装的是内置类型的元素,有没有空间配置器并没有太大影响,但当我们把一个类当作容器的元素类型时,就会发生下面的情况:
可以看到容器并没有对容器进行任何操作,仅仅只是定义了一个容器,就调用了十次Test的构造函数。如果定义初始的容量特别大,我们什么都没做,就要调用对应多次的构造函数,在退出作用域也要调用对应多次的析构函数。
而发生这种情况的原因,就是我们在构造vector数组的时候调用了_first = new T[size];
,析构的时候调用delete[]_first
。这里的new除了申请空间外,还会调用调用每一个对象的构造函数,delete也是如此。
我们想要的是在初始化容器的时候,只给容器分配空间,在插入元素的时候再构造对象,在析构容器的时候只需要析构有效的元素,再释放容器的空间。
在pop_back()操作时,只是进行了_last操作,并没有析构对象。如果我们加上delete时,除了会析构对象外,还会把对象所占的空间也释放掉。我们想要的是只析构对象,不释放对象所占的空间。
空间配置器的作用,就是把new操作的内存开辟和对象构造分离,把delete操作的内存释放和对象析构分离开。它需要做的只有四件事:内存开辟、内存释放、对象构造、对象析构。
allocator空间配置器的定义如下:
template<typename T>
struct Allocator
{
T* allocate(size_t size)
{
return (T*)malloc(sizeof(T) * size);
}
void deallocate(void* p)
{
free(p);
}
void construct(T* p, const T& val)
{
new (p) T(val);
}
void destroy(T* p)
{
p->~T();
}
};
重新改造vector容器,把模板改为template<typename T, typename Alloc = Allocator<T>>
,容器增加一个空间配置器成员对象Alloc _allocator;
。
容器构造初始化时,我们不再使用new来申请空间,而使用空间配置器的T* allocate(size_t size)
方法来申请内存,这样在就不会在没有元素的时候还是会调用构造函数的情况。
容器析构时,也不再使用delete来释放,而是先逐个把有效元素析构,再释放容器的内存。
加上空间配置器的vector容器代码:
#include <iostream>
using namespace std;
template<typename T>
struct Allocator
{
T* allocate(size_t size)
{
return (T*)malloc(sizeof(T) * size);
}
void deallocate(void* p)
{
free(p);
}
void construct(T* p, const T& val)
{
new (p) T(val);
}
void destroy(T* p)
{
p->~T();
}
};
//template<typename T>
template<typename T, typename Alloc = Allocator<T>>
class vector
{
public:
vector(int size = 10)
{
//_first = new T[size];
_first = _allocator.allocate(size);
_last = _first;
_end = _first + size;
}
~vector()
{
//delete[] _first;
for (T* p = _first; p != _last; p++)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
_first = nullptr;
_last = nullptr;
_end = nullptr;
}
vector(const vector<T>& vec)
{
int size = vec._end - vec._first;
int len = vec._last - vec._first;
//_first = new T[size];
_first = _allocator.allocate(size);
for (int i = 0; i < len; i++)
{
//_first[i] = vec._first[i];
_allocator.construct(_first + i, vec._first[i]);
}
_last = _first + len;;
_end = _first + size;
}
vector<T>& operator=(const vector<T>& vec)
{
if (this == &vec)
return *this;
//delete[] _first;
for (T* p = _first; p != _last; p++)
{
_allocator.destroy(p);
}
_allocator.deallocate(_first);
int size = vec._end - vec._first;
int len = vec._last - vec._first;
_first = new T[size];
for (int i = 0; i < len; i++)
{
//_first[i] = vec._first[i];
_allocator.construct(_first + i, vec._first[i]);
}
_last = _first + len;;
_end = _first + size;
return *this;
}
void push_back(const T& val)
{
if (isFull())
expend();
//*_last++ = val;
_allocator.construct(_last++, val);
}
void pop_back()
{
if (isEmpty())
return;
//_last--;
_allocator.destroy(--_last);
}
T back()const
{
return *(_last - 1);
}
bool isEmpty()const { return _last == _first; }
bool isFull()const { return _end == _last; }
int size()const { return _last - _first; }
private:
void expend()
{
int len = _end - _first;
//T* tmp = new T[len * 2];
T* tmp = _allocator.allocate(len * 2);
for (int i = 0; i < len; i++)
{
//tmp[i] = _first[i];
_allocator.construct(tmp + i, _first[i]);
}
delete[]_first;
_first = tmp;
_last = _first + len;
_end = _first + len * 2;
}
private:
T* _first; //数组起始地址
T* _last; //最后一个有效元素的后继位置
T* _end; //数组空间的后继位置
Alloc _allocator; //容器空间配置器对象
};
class Test
{
public:
Test() { cout << "Test()" << endl; }
Test(const Test& t){ cout << "Test(const Test&)" << endl; }
~Test() { cout << "~Test()" << endl; }
};
int main()
{
Test t1, t2, t3;
cout << "============================" << endl;
vector<Test> vec;
vec.push_back(t1);
vec.push_back(t2);
vec.push_back(t3);
cout << "============================" << endl;
vec.pop_back();
cout << "============================" << endl;
}
使用空间配置器后的效果: