vector预分配空间溢出
vector
当一个vector预分配的存储空间用完之后,为维护其连续的对象数组,它必须在另外一个地方重新分配大块新的(更大的)存储空间,并把以前已有的对象拷贝到新的存储空间中去。
// A class to track various object activities
#ifndef NOISY_H
#define NOISY_H
#include<iostream>
using std::endl;
using std::cout;
using std::ostream;
class Noisy{
//设置一些static变量用来跟踪所有的创建,赋值(使用运算符operator=), 拷贝构造和析构操作
static long create, assign, copycons, destroy;
long id;
public:
Noisy() :id(create++)//
{
cout << "d[" << id << "]" << endl;
}
Noisy(const Noisy& rv) :id(rv.id)
{
cout << "c[" << id << "]" << endl;
++copycons;
}
Noisy& operator=(const Noisy& rv){//赋值运算符operator=
cout << "(" << id << ")=[" << rv.id << "]" << endl;
id = rv.id;
++assign;
return *this;
}
//为了支持排序和查找,Noisy 必须有运算符operator< 和 operator=
//这仅仅是比较id值
friend bool operator==(const Noisy& lv, const Noisy& rv)
{
return lv.id == rv.id;
}
friend bool operator<(const Noisy& lv, const Noisy& rv)
{
return lv.id < rv.id;
}
~Noisy(){
cout << "~[" << id << "]" << endl;
++destroy;
}
friend ostream& operator<<(ostream& os, const Noisy& n)
{
return os << n.id;
}
friend class NoisyReport;
};
//Ojects of type NoisyGen are funciton objects(since there is an operator())
//that produce Noisy objects during testing
struct NoisyGen{
Noisy operator()(){
return Noisy();
}
};
//a singleton will automatically report the statistics as the program terminates
class NoisyReport{
static NoisyReport nr;
NoisyReport(){}//private constructor
NoisyReport& operator=(NoisyReport&);//disallowed
NoisyReport(const NoisyReport&);//disallowed
public:
//beause we only want one report printed at program termination.
//It has a private constructor so no additional NoisyReport objects can be created
//it disallows assignment and copy-construction,
//and it has a single static instance of NoisyReport called nr.
//the only executable statements are in the destructor, which is called as the program
//exits
//and static destructors are called.
//the distructor printss the statistics captured by the static variables in Noisy
~NoisyReport()
{
cout << "\n.................\n"
<< "Noisy creations: " << Noisy::create
<< "\nCopy-Constructions: " << Noisy::copycons
<< "\nAssignment: " << Noisy::assign
<< "\nDestructions: " << Noisy::destroy << endl;
}
};
#endif
#include"Noisy.h"
long Noisy::create = , Noisy::assign = , Noisy::copycons = , Noisy::destroy = ;
NoisyReport NoisyReport::nr;
//using Noisy.h, the following program shows a vector overflowing
#include<iostream>
#include<string>
#include<vector>
#include<cstdlib>
#include"Noisy.h"
using namespace std;
int main(int argc, char* argv[])
{
int size = ;
if (argc >= ) size = atoi(argv[]);
vector<Noisy> vn;
Noisy n;
for (int i = ; i < size; i++)
vn.push_back(n);
cout << "\n clearning up" << endl; }
测试结果

Note that the use of reserve( ) is different from using the vector constructor with an integral first argument; the latter initializes a prescribed number of elements using the element type’s default constructor.
是不是说
vector<Noisy> v(100)这用形式调用100次构造函数
而reverse其实是不调用的,只预留空间
The deque also allows random access with operator[ ], but it’s not quite as fast as vector’s operator[ ].
vector预分配空间溢出的更多相关文章
- vecor预分配内存溢出2
vector预分配内存溢出导致原始的 迭代器 失效 consider what happens when you add the one additional object that causes t ...
- Java堆空间溢出解决方法 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
一般通过java -jar filename.jar运行jar包,但是当运行的java程序需要较大的内存时,可能会造成堆空间溢出. 例如,加载了几个G大小的json文件,运行报错: Exception ...
- STL—vector空间的动态增长
vector空间的动态增长 当添加元素时,如果vector空间大小不足,则会以原大小的两倍另外配置一块较大的新空间,然后将原空间内容拷贝过来,在新空间的内容末尾添加元素,并释放原空间.vect ...
- STL空间配置器、vector、list、deque、map复习
本文写于2017-03-03,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6440830.html STL的六大组件:容器.算法.迭代 ...
- vector中的resize与 reserve
void reserve (size_type n); reserver函数用来给vector预分配存储区大小,即capacity的值 ,但是没有给这段内存进行初始化.reserve 的参数n是推荐预 ...
- stl——vector详解
stl——vector详解 stl——vector是应用最广泛的一种容器,类似于array,都将数据存储于连续空间中,支持随机访问.相对于array,vector对空间应用十分方便.高效,迭代器使ve ...
- java内存溢出的解决思路
原文地址:https://www.cnblogs.com/200911/p/3965108.html 内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终使得程序运行要用到的内存大于虚拟机能 ...
- vector at()函数比 []运算符操作安全
转载:https://blog.csdn.net/chenjiayi_yun/article/details/18507659 []操作符的源码 reference operator[](size_t ...
- 【校招面试 之 C/C++】第20题 C++ STL(二)之Vector
1.vector的动态增长 当添加元素时,如果vector空间大小不足,则会以原大小的两倍另外配置一块较大的新空间,然后将原空间内容拷贝过来,在新空间的内容末尾添加元素,并释放原空间.vector的空 ...
随机推荐
- Web开发需要关注的技术细节
摘要:在网站发布前,开发者需要关注有许多的技术细节,比如接口设计.用户体验.安全性.Web标准.性能.SEO等,倘若一个疏忽就会影响到整体的体验效果.作为一名Web开发者,哪些技术细节需要考虑呢? [ ...
- 主流智能手机屏幕材质介绍 及 LCD闪屏现象分析
TN TN(Twisted Nematic) 即扭曲向列型面板,属于有源矩阵液晶显示器中的一种.由于TFT是主动式矩阵LCD可让液晶的排列方式具有记忆性,不会在电流消失后马上恢复 ...
- 完美转换MySQL的字符集 Mysql 数据的导入导出,Mysql 4.1导入到4.0
MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...
- 【转】【opencv】仿射变换
仿射变换 目标 在这个教程中你将学习到如何: 使用OpenCV函数 warpAffine 来实现一些简单的重映射. 使用OpenCV函数 getRotationMatrix2D 来获得一个 旋转矩阵 ...
- Apache 虚拟主机 VirtualHost 配置
虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...
- Command-line interface
A command-line interface (CLI), also known as command-line user interface, console user interface, a ...
- idea静态数据加载失败的解决办法
把上图中的resource文件夹(存放xml等配置文件)标记成resource.
- MINA2.0原理
转自:http://blog.csdn.net/liuzhenwen/article/details/5894279 客户端通信过程 1.通过SocketConnector同服务器端建立连接 2. ...
- maven项目文件乱码问题
今日碰到maven项目中的一个资源文件出现乱码,排查发现是在.setting文件夹下的org.eclipse.core.resources.prefs文件导致的. 修改前的编码规则如下: <sp ...
- request对象方法详解
自己整理的 javax.servlet.http.HttpServletrequest 所有方法,欢迎收藏! 方法名 说明 isUserInRole 判断认证后的用户是否属于某一成员组 getAttr ...