参考资料:
1. https://blog.csdn.net/luomoshusheng/article/details/48226517
2. https://blog.csdn.net/wangshubo1989/article/details/49913239 (妙得很)
3. https://www.cnblogs.com/biyeymyhjob/archive/2012/09/12/2674004.html
贴两段代码防忘记:
当存储类对象的指针时,析构和结构的过程分析:
#include
#include
using namespace std;
class Test
{
public:
Test(void);
~Test(void);
};
Test::Test(void) {
cout << "object created\n";
}
Test::~Test(void) {
cout << "-->object deleted\n";
}
int main() {
vector vec;
Test* t1 = new Test();
Test* t2 = new Test();
Test* t3 = new Test();
Test* t4 = new Test();
Test* t5 = new Test();
Test* t6 = new Test();
Test* t7 = new Test();
Test* t8 = new Test();
vec.push_back(t1);
cout << vec.size() << endl;
vec.push_back(t2);
cout << vec.size() << endl;
vec.push_back(t3);
cout << vec.size() << endl;
vec.push_back(t4);
cout << vec.size() << endl;
vec.push_back(t5);
cout << vec.size() << endl;
vec.push_back(t6);
cout << vec.size() << endl;
vec.push_back(t7);
cout << vec.size() << endl;
vec.push_back(t8);
cout << vec.size() << endl;
cout << "before deleting ... " << endl;
for (int i = 0; i < vec.size(); ++i) {
delete vec[i];
}
cout << "after deleting, before clear ... " << endl;
vec.clear();
return 0;
}
所得结果为:
object created
object created
object created
object created
object created
object created
object created
object created
1
2
3
4
5
6
7
8
before deleting ...
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
after deleting, before clear ...
在这个过程中,首先8个类对象及指向该对象的指针都被初始化好,在初始化类对象的过程中,构造函数被调用,8个“object created” 被打印出来。
然后分别放入vector中,此时vector存放的只是指针,并不存在类对象的copy和析构过程,所以析构函数打印在显示vector size的时候没有出现。
由资料1和3可知,当vector中存储的元素为类对象的指针时,因为指针无法被vector用来调用析构函数,所以调用clear只是清空vector size为0,并没有空间的释放,需要在调用clear函数之前,遍历并释放。从得到的结果也可以看出,类对象的空间是在调用delete的时候被释放的。
当存储类对象时,析构和结构的过程分析:
#include
#include
using namespace std;
class Test
{
public:
Test(void);
Test(const Test& t);
~Test(void);
};
Test::Test(void) {
cout << "object created\n";
}
Test::Test(const Test& t) {
cout << "copy\n";
}
Test::~Test(void) {
cout << "-->object deleted\n";
}
int main() {
vector vec;
Test t1;
Test t2;
Test t3;
Test t4;
Test t5;
Test t6;
Test t7;
Test t8;
vec.push_back(t1);
cout << vec.size() << endl;
vec.push_back(t2);
cout << vec.size() << endl;
vec.push_back(t3);
cout << vec.size() << endl;
vec.push_back(t4);
cout << vec.size() << endl;
vec.push_back(t5);
cout << vec.size() << endl;
vec.push_back(t6);
cout << vec.size() << endl;
vec.push_back(t7);
cout << vec.size() << endl;
vec.push_back(t8);
cout << vec.size() << endl;
cout << "after deleting, before clear ... " << endl;
vec.clear();
cout << "after clear ... " << endl;
cout << vec.capacity() << endl;
vector(vec).swap(vec);
cout << vec.capacity() << endl;
return 0;
}
所得结果为:
object created
object created
object created
object created
object created
object created
object created
object created
push 1
copy
1
push 2
copy
copy
-->object deleted
2
push 3
copy
copy
copy
-->object deleted
-->object deleted
3
push 4
copy
4
push 5
copy
copy
copy
copy
copy
-->object deleted
-->object deleted
-->object deleted
-->object deleted
5
push 6
copy
6
push 7
copy
7
push 8
copy
8
after deleting, before clear ...
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
after clear ...
8
0
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
-->object deleted
由资料1和资料3可知,在vector分配内存的时候,所得到的存储空间总是大于等于所需存储空间:
即 const size_type len=old_size !=0?2*old_size :1;
由结果可看出,在写入第一个对象的时候,vector其实分配了1个类对象的存储空间。
在写入第二个对象的时候,由于存储空间不够,vector申请了二倍于原来size的存储空间,也就是两个类对象的大小,copy1 copy2, 然后清空旧的1。
在写入第三个对象的时候,由于存储空间不够,vector申请了二倍于原来size的存储空间,也就是四个类对象的大小,copy1,copy2,copy3,然后清空旧的1和2。
在写入第四个对象的时候,存储空间足够,没有再新申请空间,而是直接copy4。
之后的情况依次类推。
之后,由于存储的是类对象而没有用到指针,自然不需要delete。但从结果可以看出,clear也并没有释放掉vector占用的内存,这个时候使用资料2和资料3中提到的方法,使用swap与局部变量交换的方法释放掉vector占用的全部内存。可见vector的capacity由8变为0。
最后,在main函数结束,vector的析构被调用,类的析构被调用8次。
(先写这些,感觉还有漏洞,想到再补)