在《STL 源码剖析 -- 侯捷》书籍分析的 tass-sgi-stl-2.91.57-source 源码中。
对 vector 的 erase(); 函数有疑问:
- iterator erase(iterator position) {
- if (position + 1 != end())
- {
- //把从 position+1 到 finish 之间的元素一个一个复制到从 position 指向
- //的空间,这样,就把 position 原来指向的元素个覆盖了
- copy(position + 1, finish, position);
- }
-
- --finish;
- destroy(finish);
- return position;
- }
//=====================================================================
//该函数把 position 指向的对象删除
//但是,注意(2013-6-24 wkf):
//自己认为该函数有一个漏洞,就是: 该函数没有真正地销毁 position 指向的对象,
//没有执行它的析构函数。
//假设有如下的一个列表,其内存空间表示如下:
//---------------------------------------------------------
//| 1 | 2 | 3 | 4 | 5 | | |
//---------------------------------------------------------
// |
// finish
//现在,要删除第二个元素 2 这个对象。
//所以,执行 copy 之后,把 3, 4, 5 对象往前移动,其内存空间如下:
//---------------------------------------------------------
//| 1 | 3 | 4 | 5 | 5 | | |
//---------------------------------------------------------
// |
// finish
//注意,第 5 个元素的内存空间还是原来的对象 5 这个对象。
//然后,执行 --finish; 操作,移动指针,如下:
//---------------------------------------------------------
//| 1 | 3 | 4 | 5 | 5 | | |
//---------------------------------------------------------
// |
// finish
//然后,执行 destroy(finish); 操作,执行的是第 5 个元素的析构函数,而
//我们删除的第2个对象,并没有执行其析构函数,只是把它在内存空间给覆盖了。
//如果一个对象中有 动态分配 的内存空间,就不会被释放。
//因为,我们都习惯于在 构造函数中使用 new 来分配内存,在析构函数中使用 delete 来
//释放内存。
//=====================================================================
如下是一个测试的的例子:
- class test
- {
- public:
- int i;
- public:
- test(int a)
- {
- i = a;
- cout << "construct i = " << i << endl;
- }
- test(const test &a)
- {
- i = a.i;
- cout << "copy construct i = " << i << endl;
- }
- ~test()
- {
- cout << "=== destruct i = " << i << endl;
- }
- };
- void show(vector<test>& num)
- {
- vector<test>::iterator index;
- index = num.begin();
- while(num.end() != index)
- {
- cout << (*index).i << " ";
- index++;
- }
- cout << endl;
- }
-
- void main()
- {
-
- vector<test> num;
- for(int i = 0; i < 6; i++)
- {
- num.push_back(test(i));
- }
-
- show(num);
- num.erase(num.begin()+1);
- show(num);
- num.erase(num.begin()+1);
- show(num);
- num.erase(num.begin()+1);
- show(num);
-
- printf("hehe.....\n");
- getchar();
- return;
- }
运行的结果如下:
construct i = 0
copy construct i = 0
=== destruct i = 0
construct i = 1
copy construct i = 0
copy construct i = 1
=== destruct i = 0
=== destruct i = 1
construct i = 2
copy construct i = 0
copy construct i = 1
copy construct i = 2
=== destruct i = 0
=== destruct i = 1
=== destruct i = 2
construct i = 3
copy construct i = 0
copy construct i = 1
copy construct i = 2
copy construct i = 3
=== destruct i = 0
=== destruct i = 1
=== destruct i = 2
=== destruct i = 3
construct i = 4
copy construct i = 0
copy construct i = 1
copy construct i = 2
copy construct i = 3
copy construct i = 4
=== destruct i = 0
=== destruct i = 1
=== destruct i = 2
=== destruct i = 3
=== destruct i = 4
construct i = 5
copy construct i = 5
=== destruct i = 5
0 1 2 3 4 5
=== destruct i = 5
0 2 3 4 5
=== destruct i = 5
0 3 4 5
=== destruct i = 5
0 4 5
hehe.....
重点是后面的输出:
=== destruct i = 5
0 1 2 3 4 5
=== destruct i = 5
0 2 3 4 5
=== destruct i = 5
0 3 4 5
=== destruct i = 5
0 4 5
执行的析构函数并不是需要删除的那个对象。