vector中push_back后iterator失效程序崩溃的原因


vector的push_back操作

在c++ reference中,对push_back的描述如下:

void push_back(const value_type&val);
void push_back(value_type && val);

Add element at the end
Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element.
This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.

push_back的作用是在vector的末尾添加一个新元素。val的内容被复制(或移动)到新元素。
这有效地将容器大小增加一,当且仅当新的vector大小超过当前vector容量时,会重新自动分配新的存储空间。

在进行demo测试之前,再了解另外两个函数:
std::vector::size
vec.size() 返回vec中元素的个数

std::vector::capacity
vec.capacity() 返回vec在内存中分配的空间大小

Return size of allocated storage capacity
Returns the size of the storage space currently allocated for the vector, expressed in terms of elements.
This capacity is not necessarily equal to the vector size. It can be equal or greater, with the extra space allowing to accommodate for growth without the need to reallocate on each insertion.
Notice that this capacity does not suppose a limit on the size of the vector. When this capacity is exhausted and more is needed, it is automatically expanded by the container (reallocating it storage space). The theoretical limit on the size of a vector is given by member max_size.
The capacity of a vector can be explicitly altered by calling member vector::reserve.


push_back操作的demo

//code1

#include <iostream>
#include <vector>
using namespace std;

#define MAX_NUM 9

int main(){
	vector<int> vecInt;
	
	for(int i = 0; i != MAX_NUM; i++){
		vecInt.push_back(i);
	}
	/**
		some code
	*/
	vecInt.push_back(123);

	for(int i : vecInt){
		cout << i << " ";
	}
	
	return 0;
}

以上代码先声明了一个存放int类型的vector,然后把i递增push_back到vecInt中。之后再添加一个元素123到vecInt中。
此时,用c++11的范围for来遍历这个vecInt,会得到:

0 1 2 3 4 5 6 7 8 123

这是意料之中的事情,换用iterator来打印这个vector试试:


iterator遍历vector

//code2

#include <iostream>
#include <vector>
using namespace std;

#define MAX_NUM 9

int main(){
	vector<int> vecInt;
	
	for(int i = 0; i != MAX_NUM; i++){
		vecInt.push_back(i);
	}

	vector<int>::iterator iter = vecInt.begin();
	cout << "the 1st element: " << *iter << endl;
	
	vecInt.push_back(123);
	
	while(iter != vecInt.end()){
		cout << *iter << " ";
		iter++;
	}
	
	return 0;
}

在用for进行vector的push_back之后,初始化了一个iterator指向vecInt的begin位置,并打印验证。之后再用push_back在vector的末尾添加了一个元素123,这时候用iter来遍历vecInt。

得到的结果和code1的运行结果一样,如下图,符合预期:
在这里插入图片描述


ATTENTION! 注意!

下面对MAX_NUM进行修改,将其改为8

//code3
#include <iostream>
#include <vector>
using namespace std;

#define MAX_NUM 8	//MAX_NUM修改为8,其余地方不做任何修改

int main(){
	vector<int> vecInt;
	
	for(int i = 0; i != MAX_NUM; i++){
		vecInt.push_back(i);
	}

	vector<int>::iterator iter = vecInt.begin();
	cout << "the 1st element: " << *iter << endl;
	
	vecInt.push_back(123);
	
	while(iter != vecInt.end()){
		cout << *iter << " ";
		iter++;
	}
	
	return 0;
}

我们只修改MAX_NUM的值,其他地方保留和code2一样,再次运行的时候,程序崩溃了
(或者,打印第一行the 1st element: 0,打印第二行0 1 2 3 4 5 6 7 之后是乱码,等)


原因分析

再回到文章开始推push_back操作的说明:
当且仅当新的vector大小超过当前vector容量时,会重新自动分配新的存储空间。

对于vector来说,和数组最大的区别之一,就是不需要在初始化的时候声明vector的大小。如果初始化的时候没有指明vector的大小,那么会根据实际的使用情况,在内存中为vector分配的大小分别 2 -> 4 -> 8 -> 16 … 。

那么在code3中,MAX_NUM是8,所以在for进行push_back之后,vecInt在内存中的大小为8。 对vecInt再次将元素123进行push_back的时候,新的vector大小将超过当前的vector大小,所以会自动重新分配存储空间。

由于vector的存储空间已经被重新分配,在push_back(123)之后,iter自然也就会指向一个未知的空间。所以会导致程序异常。


代码验证

那么重新自动分配大小是多大呢?为什么MAX_NUM为9的时候不会出现这种情况呢?可以使用capacity()验证

#include <iostream>
#include <vector>
using namespace std;

#define MAX_NUM 8

int main(){
	vector<int> vecInt;
	
	for(int i = 0; i != MAX_NUM; i++){
		vecInt.push_back(i);
	}
	cout << vecInt.capacity() << endl;

	vector<int>::iterator iter = vecInt.begin();
	cout << "the 1st element: " << *iter << endl;
	
	vecInt.push_back(123);
	cout << vecInt.capacity() << endl;

//	先注释掉会崩溃的代码
//	while(iter != vecInt.end()){
//		cout << *iter << " ";
//		iter++;
//	}
	
	return 0;
}

结果如下:
在这里插入图片描述

当MAX_NUM为8的时候,在for进行push_back之后,vector的占用的空间是8,再次push_back(123)之后,vector所占空间变成了16重新自动分配了内存空间,所以iterator会失效。

修改MAX_NUM为9,执行结果如下:
在这里插入图片描述

在一开始的时候vector所占用的空间是16,进行push_back的时候空间足够,所以不需要重新分配,所以iterator依然有效,可以用iterator遍历vector。

附完整代码:

#include <iostream>
#include <vector>
using namespace std;

#define MAX_NUM 9

int main(){
	vector<int> vecInt;
	
	for(int i = 0; i != MAX_NUM; i++){
		vecInt.push_back(i);
	}
	cout << "capacity: " << vecInt.capacity() << endl;

	vector<int>::iterator iter = vecInt.begin();
	cout << "the 1st element: " << *iter << endl;
	
	vecInt.push_back(123);
	cout << "capacity: " << vecInt.capacity() << endl;
	
	while(iter != vecInt.end()){
		cout << *iter << " ";
		iter++;
	}
	
	return 0;
}

其他MAX_NUM为2^n的时候也会发生类似的情况


总结

在使用push_back对vector进行构造的时候,vector的容量capacity(与size有区别)会根据压入元素的数量进行内存的自动重新分配,这时候iterator会因为vector存储空间的变化而失效,需要注意iterator的有效性,以免iterator指向未知的内存空间导致程序异常。

  • 22
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值