vecor预分配内存溢出2
2016-07-25 10:35
225 查看
vector预分配内存溢出导致原始的 迭代器 失效
consider what happens when you add the one additional object that causes the vector to reallocate storage and move it elsewhere. The iterator’s pointer is now pointing off into nowhere:
This illustrates the concept of iterator invalidation. Certain operations cause internal changes to a container’s underlying data, so any iterators in effect before such changes may no longer be valid afterward.
consider what happens when you add the one additional object that causes the vector to reallocate storage and move it elsewhere. The iterator’s pointer is now pointing off into nowhere:
This illustrates the concept of iterator invalidation. Certain operations cause internal changes to a container’s underlying data, so any iterators in effect before such changes may no longer be valid afterward.
#include<iostream> #include<iterator> #include<vector> using namespace std; int main() { vector<int> vi(10, 0); ostream_iterator<int> out(cout, " "); vector<int>::iterator i = vi.begin(); *i = 47; copy(vi.begin(), vi.end(), out); cout << endl; //force it to move memory(could also just add enough objects) vi.resize(vi.capacity() + 1); //now i points to wrong memory *i = 48; copy(vi.begin(), vi.end(), out);//no change to vi[0] }
![](https://images2015.cnblogs.com/blog/724691/201607/724691-20160725103303263-1072860914.png)
相关文章推荐
- 让VS2013添加新类时自动添加public关键字
- 2016/7/25 D180C SPI flash增加代码记录
- 动态路由 RIP
- 细谈select函数(C语言)
- 细谈select函数(C语言)
- 微信接口CSDN博客
- java学习资源分享
- jvm类加载器
- Count and Say
- 在JS中用EL表达式访问request作用域里面的值得问题
- DB2 新增主键
- form表单关闭历史记录
- 7.20_计算机基础知识
- nncq---后台所有文章添加一个发布日期修改
- eclipse启动tomcat无法访问
- snprintf函数用法
- 区块链:起源、原理及应用
- nefu 2哥德巴赫猜想
- Ubuntu 安装 python3.5
- css实现百分比进度条