.在上一节我们实现的 MyVector存在哪些问题?

问题1

现在有Student类
class Student{
public:
Student(){cout<<"构造Student对象"<<endl;}
~Student(){cout<<"析构Student对象"<<endl;}
private:
int age
char NAME[20;]
char *pAddress;
} MyVector<Student> v1[1000];

我只是希望创建一个能放1000个Student 的Vector,但是开始并不放任何内容,但是发现编译器除分配了1000个student对象的空间,还创建了1000个对象,

在main函数结束后再析构这1000个对象,这就是问题1,这1000个对象并不是我需要的,

原因如下,在MyVector的构造函数中 T * _tep = new T[size](); 这个new除了分配空间,还会调用对象的构造函数完成对象的初始化

换句话说就是 空间的分配和对象的创建联系在了一起,这个非常不合理,我们需要把他分开,我希望是需要帮我开辟空间即可,不希望帮我创建1000个对象

问题2关于析构函数

//析构函数
~MyVector<T>() {
if (Empty() == false) {
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr;
cout << "析构Vector,堆地址"<<first << endl;
} }

实际情况时,我的vector是可以放1000个student对象的容器,但是实际里面可能暂时只放了100个对象,

而delete[] first,是把整个vector中的1000个student对象都调用析构函数,这也不合理,我们只需要析构有效

数量的对象然后再释放first指针指向的堆内存

问题3 关于添加元素和删除元素

student s1;
student s2;
student s3; MyVector<student> v1(1000);// 这一句编译器会先分配1000个student对象空间,然后再调用1000次构造函数创建1000个student对象 v1.pushback(s1);
//上面这句话会调用赋值函数,会将s1的内容赋值给已经在堆上的student对象,但实际上我想要的是,只需要将分配好的空间
//给我,我使用拷贝构造,将s1拷贝构造出来一个对象放在你给的空间上,这是个问题 v1.popBack(){
this->last--;
}
//上面的pop最后一个studnet对象,只是将最后一个元素的指针前移动一个student空间大小,并没有去析构这个对象,
//而且这个弹出的student对象里面有一个char *pAddress 指向了外部堆内存资源,这个内存自己没有去释放的话,
//造成了内存泄漏了. 这个也是个问题,所以说我们要去析构这个弹出的student对象但是不用使用 delete 方式 delete 这个对象,为什么呢?
//应为delete 处理调用析构函数完,他还要释放内存空间,但是这个空间属于Vector的,释放掉了这一小块空间,我们后面就无法在使用了,
//**所以一句话就是我们从vector中删除一个对象时,只做析构这个对象而不释放他的内存空间,把对象的析构和内存的释放分开**

以上三个问题使得我们不能采用上一篇文字中的方式写Vector,这就是引入了容器的空间配置器的原因

容器的空间配置器做了4件事

内存开辟/内存释放 对象创建/对象析构

在上一遍的基础上加入空间配置器,代码如下

#include <iostream>
using namespace std; class student { public: student(int _age, char * _pname):
age(_age),
pName(_pname){
} student(const student & _rvalue) {
this->age = _rvalue.age;
this->pName = new char[20];
strcpy(this->pName, _rvalue.pName);
} student & operator =(const student & _rvalue) { if (this == &_rvalue) { return *this; } delete[]this->pName;
this->pName = nullptr; this->age = _rvalue.age;
this->pName = new char[20];
strcpy(this->pName, _rvalue.pName);
}
~student() {
delete[] this->pName;
this->pName == nullptr;
cout << "student 析构函数被执行,当前对象地址是" << this << endl;
} private:
int age;
char *pName;
}; template<typename T>
class MyAllocate { public: T * allocate(int size) {
return (T *)malloc(sizeof(T)*size);
} //根据指定的地址,释放内存空间
void delAllocate(T *p) {
free(p);
} //在p指针指定的位置,根据指定的对象创建新对象
void construct(T * p, const T & _rValue) {
new (p) T(_rValue);
} //析构指定对象,但不释放内存空间
void destory(T * p) {
if (p != nullptr) {
p->~T();
}
} }; template<typename T, typename Allocate=MyAllocate<T>>
class MyVector2 { public: //构造函数
MyVector2<T, Allocate>(int size = 10, const Allocate & _allocate = MyAllocate<T>()) : allocator(_allocate) { first = allocator.allocate(size);
last = first;
end = first + size;
cout << "MyVector2 构造函数,构建数量=" << size <<"堆空间构造起始地址="<<first<<"结束地址=" << endl;
} //拷贝构造,根据指定的 MyVector2 创建新的MyVector2
MyVector2<T, Allocate>(const MyVector2<T,Allocate> & _rValue) { //1:根据原MyVector2的Size 申请内存空间
first = allocator.allocate(_rValue.Size());
last = first;
end = first + __rValue.Size(); //2:根据原MyVector2内的对象,在第1步申请的堆内存中构造对象
T *tep = _rValue.first;
while (tep<_rValue.end)
{
allocate.construct(last, *tep)
last++;
tep++;
}
cout << "MyVector2 拷贝构造函数,构建数量" << __rValue.Size() << endl;
} //赋值函数
MyVector2<T, Allocate> & operator=(const MyVector2<T, Allocate> & _rValue) {
if (this == &_rValue) { return *this;} //1:先析构目标Vector2中所有的对象
T * tep = first;
while (tep < last) {
allocator.destory(tep);
tep++;
} //2:释放目标Vector2的内存空间
allocator.delAllocate(first); //3:根据原MyVector2的size 申请新的内存空间
int rSize = _rValue.Size();
T * _head allocator.allocate(rSize);
first = _head;
last = _head;
end = first + rSize; //4:根据原MyVector2中的有效的对象在 第3步申请的内存空间上构建对象 T *tep = _rValue.first;
while (tep<_rValue.end)
{
allocator.construct(last, *tep)
last++;
tep++;
} cout << "MyVector2 赋值函数,构建数量" << rSize << endl; } //在已经申请空间的位置 添加值
void pushBack(const T & _addValue) {
if (Full()) {
Expend();//两倍扩容
}
//在指定地址空间 构建对象
allocator.construct(last, _addValue);
cout << "pushBack 元素,内存地址=" << last << endl;
last++; } //弹出
void popBack() {
if (Empty()) { return; }
//1:只析构指定位置对象,但不释放空间
allocator.destory(last - 1);
cout << "popBack元素,其内存地址=" << (last - 1) << endl; //2:移动last指针
last--; } //是否为空
bool Empty() { return first == last; } //是否满
bool Full() { return last == end; } int Size() { return end - first; } //容器扩容
void Expend() { int newSize = this->Size() * 2;//两倍扩容 //1:申请新的空间
T * head = allocator.allocate(newSize);
T * tep_first = head;
T * tep_last = head;
T * tep_end = first + newSize; cout << "两倍扩容,新的堆内存起始地址=" << head << endl; //2:将原来有效的对象 赋值到新的堆空间上
T * tep = first;
while (tep < last) {
allocator.construct(tep_first,*tep);
cout << "两倍扩容,赋值对象,原对象地址=" << tep <<"目标对象地址="<<tep_first<< endl;
tep_first++;
tep_last++;
tep++;
} tep = first;
//3:析构原堆空间上有效的对象
while (tep < last) {
allocator.destory(tep);
cout << "两倍扩容,析构对象,对象地址=" << tep << endl;
tep++; } //4:释放堆上的空间
allocator.delAllocate(first);
cout << "两倍扩容,销毁原空间" << first << endl;
first = head;
last = tep_last;
end = first + newSize; } void showVectorInfo() {
T * tep = first;
while (tep < last)
{
cout << "打印Vector中有效对象地址=" << tep << endl;
tep++;
}
} private:
T * first;
T * last;
T * end;
Allocate allocator; }; int main() { MyVector2<student, MyAllocate<student>> v(4); student s1(20, "zs1"); v.pushBack(s1); student s2(22, "zs2"); v.pushBack(s2); student s3(23, "zs3"); v.pushBack(s3); student s4(24, "zs4"); v.pushBack(s4); v.showVectorInfo(); student s5(25, "zs5"); v.pushBack(s5); v.showVectorInfo(); system("pause"); return 1;
}

<四>理解空间配置器allocator, 优化STL 中的Vector的更多相关文章

  1. C++ 空间配置器(allocator)

    C++ 空间配置器(allocator) 在STL中,Memory Allocator 处于最底层的位置,为一切的 Container 提供存储服务,是一切其他组件的基石.对于一般使用 STL 的用户 ...

  2. STL源码剖析——空间配置器Allocator#2 一/二级空间配置器

    上节学习了内存配置后的对象构造行为和内存释放前的对象析构行为,在这一节来学习内存的配置与释放. C++的内存配置基本操作是::operator new(),而释放基本操作是::operator del ...

  3. 《STL源码剖析》chapter2空间配置器allocator

    为什么不说allocator是内存配置器而说是空间配置器,因为空间不一定是内存,也可以是磁盘或其他辅助介质.是的,你可以写一个allocator,直接向硬盘取空间.sgi stl提供的配置器,配置的对 ...

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

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

  5. C++ STL学习之 空间配置器(allocator)

    众所周知,一般情况下,一个程序包括数据结构和相应的算法,而数据结构作为存储数据的组织形式,与内存空间有着密切的联系. 在C++ STL中,空间配置器便是用来实现内存空间(一般是内存,也可以是硬盘等空间 ...

  6. STL学习笔记:空间配置器allocator

    allocator必要接口: allocator::value_type allocator::pointer allocator::const_pointer allocator::referenc ...

  7. STL源码剖析——空间配置器Allocator#1 构造与析构

    以STL的运用角度而言,空间配置器是最不需要介绍的东西,因为它扮演的是幕后的角色,隐藏在一切容器的背后默默工作.但以STL的实现角度而言,最应该首先介绍的就是空间配置器,因为这是这是容器展开一切运作的 ...

  8. STL之空间配置器allocator

    摘要 C++STL的空间配置器将内存的分配.释放,对象的构造.析构都分开执行,内存分配由alloc::allocate()负责,内存的释放由alloc::deallocate()负责:对象的构造由:: ...

  9. STL源码剖析——空间配置器Allocator#3 自由链表与内存池

    上节在学习第二级配置器时了解了第二级配置器通过内存池与自由链表来处理小区块内存的申请.但只是对其概念进行点到为止的认识,并未深入探究.这节就来学习一下自由链表的填充和内存池的内存分配机制. refil ...

  10. [C++] 空间配置器——allocator类

    1.new和delete有一些灵活性上的局限:new把内存分配和对象构造组合在了一起:delete将对象析构和内存释放组合在了一起.   2.当分配一大块内存时,我们通常计划在这块内存上按需构造对象, ...

随机推荐

  1. 使用Dockfile构建mysql镜像与初始化运行mysql容器

    使用docker 构建mysql镜像,并在容器初次创建时初始化数据 Dockerfile FROM mysql:5.7.23 MAINTAINER gradyjiang "jiangzhon ...

  2. k8s实际操作中的小知识点

    1.批量执行yaml文件 # 把所有要执行的yaml文件放在同一个目录下,并且切换到这个目录下 kubectl apply -f . 2.利用pod的亲和和反亲和功能把pod调度到不同的node上 亲 ...

  3. go-fastdfs和配套使用的web页面

    go-fastdfs go-fastdfs是一个基于http协议的分布式文件系统,它基于大道至简的设计理念,一切从简设计,使得它的运维及扩展变得更加简单,它具有高性能.高可靠.无中心.免维护等优点. ...

  4. Elasticsearch:mapping定制

  5. 第二章:视图层 - 10:动态生成PDF文件

    可以通过开源的Python PDF库ReportLab来实现PDF文件的动态生成. 一.安装ReportLab ReportLab库在PyPI上提供,可以使用pip来安装: $ pip install ...

  6. 使用nginx-ingress-controller配置https,但是再同时配置使用http

    默认情况下,如果为该 Ingress 启用了 TLS,控制器会使用 308 永久重定向响应将 HTTP 客户端重定向到 HTTPS 端口 443.( Ingress 里配置了 https 证书的话,默 ...

  7. 解析库beautifulsoup

    目录 一.介绍 二.遍历文档树 三.搜索文档树(过滤) 四.修改文档树 五.总结 一.介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的 ...

  8. PAT (Advanced Level) Practice 1001 A+B Format 分数 20

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  9. 我是加工厂,想管理生产财务采购销售这块,什么样的ERP会好用点??

    最能够贴合你的业务需求和自己员工的使用习惯的才会更好用,最好能简单快捷的进行低成本个性化定制的那种应该比较适合你这种加工厂,没有完全相同的两家企业,更别说他们的发展走向,即使同一家企业不同发展阶段.时 ...

  10. 微信小程序之发起请求

    wx.request({ url: api.api + '/weChat/api/user/myAunt', // 仅为示例,并非真实的接口地址 data: {}, method: 'GET', he ...