.在上一节我们实现的 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. 【Shashlik.EventBus】.NET 事件总线,分布式事务最终一致性

    [Shashlik.EventBus].NET 事件总线,分布式事务最终一致性 简介 github https://github.com/dotnet-shashlik/shashlik.eventb ...

  2. .NET 反向代理-YARP 根据域名转发

    前段时间发布过一个关于 YARP 的简单介绍,感兴趣的小伙伴恭请移步看看 .NET 反向代理-YARP - 一事冇诚 - 博客园 (cnblogs.com) 作为反向代理,必不可少的当然是根据域名代理 ...

  3. kibana访问多个 Elasticsearch 节点间的负载均衡

    如果 Elasticsearch 集群有多个节点,分发 Kibana 节点之间请求的最简单的方法就是在 Kibana 机器上运行一个 Elasticsearch 协调(Coordinating onl ...

  4. MySQL8.0报错:Access denied; you need (at least one of) the SYSTEM_USER privilege(s) for this operation

    MySQL8.0.16版本中新增了一个system_user帐户类型,当新增用户并赋予权限时 mysql> create user 'proxysql'@'192.168.20.%' ident ...

  5. ProxySQL(2):初试读写分离

    文章转载自:https://www.cnblogs.com/f-ck-need-u/p/9278839.html 实现一个简单的读写分离 这里通过一个简单的示例实现ProxySQL的读写分离功能,算是 ...

  6. 记录一个奇葩的问题:k8s集群中master节点上部署一个单节点的nacos,导致master节点状态不在线

    情况详细描述; k8s集群,一台master,两台worker 在master节点上部署一个单节点的nacos,导致master节点状态不在线(不论是否修改nacos的默认端口号都会导致master节 ...

  7. Java SpringBoot 项目构建 Docker 镜像调优实践

    PS:已经在生产实践中验证,解决在生产环境下,网速带宽小,每次推拉镜像影响线上服务问题,按本文方式构建镜像,除了第一次拉取.推送.构建镜像慢,第二.三-次都是几百K大小传输,速度非常快,构建.打包.推 ...

  8. Oracle基础知识汇总一

    Oracle基础知识 以下内容为本人的学习笔记,如需要转载,请声明原文链接   https://www.cnblogs.com/lyh1024/p/16720759.html oracle工具: SQ ...

  9. 5N的多次方

    N=eval(input(" 请输入一个数:")) for i in range (5): print(pow(N,i))

  10. EasyExcel实现文件导入

    导入 准备工作 见:https://www.cnblogs.com/wywblogs/p/16095576.html 异步导入 接口代码 public Map importMemberList(@Re ...