C++内存分配与对象构造的分离

在C++中,我们基本用new(delete)操作符分配(释放)内存。new操作符为特定类型分配内存,并在新分配的内存中构造该类型的一个对象。new表达式自动运行合适的构造函数来初始化每个动态分配的类类型对象。即new表达式既分配了内存同时也构造了对象。

然而,我们一定会遇到这样的情况:预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个对象。即将内存分配与对象构造分开进行,这样做的理由是:

(1)在内存分配时构造对象很浪费,可能会创建从不使用的对象。

(2)当实际使用预先分配的对象时,被使用的对象很可能要重赋新值。

string* pstr = new string[5]; 

上面举了个不合适的例子(当然你应该用vector<string>来代替),毫无疑问被分配的5个string空间是被string默认构造函数初始化了,而且接下来你肯定得对pstr[0...4]重新赋值。所以new操作符这种分配特点会增加运行时开销。尤其是某些用户的类类型要求对象分配更快一些,做法通常是:预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个新对象。

 

一、分配原始内存

C++提供两种方法分配和释放未构造的原始内存:

(1)allocator类,它提供可感知类型的内存分配。这个类支持抽象接口,以分配内存并随后使用该内存保存对象。

2)标准库中的operator new和operator delete,它们分配和释放需要大小的原始的,未类型化的内存。

1、allocator类

allocator类是一个模板,它提供类型化的内存分配以及对象构造与撤销。它支持的操作如下:

 

http://www.lofter.com/favblog/xuyiomvlc909806?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/gongsongrrzux29?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/jiluyrcsg109459?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/wangtanwqwyu024?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/shuidongqisnz76?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/michoumdzsp8231?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/guodiuogep39547?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/weizhaogodgm695?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/anwoasoja157325?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/weishunlseng710?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/caizhongxgweo57?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/shutongcznps306?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/renmenlduge2803?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/manemwbfg542858?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/jicongasdmn6365?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/pairangna8203?fpost=1d26b0e9_6a01fd0 http://www.lofter.com/favblog/zhuobikvdji1439?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jichenghszjv098?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/chaijildmhl2590?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ouyongiswwx7705?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fuhangjoqxa0051?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baolangakmwq726?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shizhikdacd2637?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanhuaibzawt014?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jirenvrcef81309?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/daidaowbzxs8484?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/rongoukrtuj3385?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fengmeitmffa771?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fenglinemldj651?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzhifygqv165?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/cailuqfeim09860?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/mieboqdjmi58267?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/moshangvfbna259?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/kongyuncrhix607?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/quqitbclo117894?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/raoshanabjek894?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/niuyoujerib0378?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haorenknoab6213?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanzenbqkod6504?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gandengdllzr709?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/guodiffunb97223?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/minbigapof86162?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fanliangcduuy54?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/buchunboauk9810?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shencangtwybd07?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lianfeiyflun841?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/naqieivcjh08483?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pangjiaoxzgdt70?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pipangjdyni8384?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xugaiwulpr80279?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xianbeibtlkm830?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/nongtangqmmg769?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuchengjxlgb61?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/changshitdpga04?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/sangturewxd0234?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yezhuanggvcer86?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuexineuoba8918?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhaoxinrwlbz437?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wupioitpl903373?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/kuishanggylrc36?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shangweibqlji73?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangxianddhes07?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangcaiuxaem184?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baidaosntec8062?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/douanodtsz49703?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuqianbeicy9190?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shuangshiayahu5?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/changnaopjtym49?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haoluxnldz65291?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gousouvtjjq3553?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuoyouvpnsu115?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pingweitzvhl353?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ruantuniuwyl382?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumeilkzna25713?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/suweigdwup20495?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ruicukfwmm19085?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shenchenbzots52?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/duyuebawea15981?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xiangguofiwa183?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baishanghjrx354?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huochenzxtag392?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gengshanpciuw42?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bugubqpto458787?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yunaioldgl66967?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/longcanglyrxw77?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzazfsvs4188?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huihanxvlmb3915?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/tongjitkzbj1208?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qudongvnsms8711?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gudongdnlie0746?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yiqiaokkxim4405?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/laocvhhu9726338?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baomeiguttv6682?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/chaoliaoapucj78?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanbeiwdrtx5074?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumorekei229629?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/keluanqnlic1065?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuyoutgflp66710?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/putangcwkai6962?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gushuikkslg8649?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bushigpfvk63124?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhengguahryna73?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiuwencqizl6631?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fengbenhutht210?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huiyuanedxnl983?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jiangyongclqst8?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pibaumrps460281?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wangdanvhzqb429?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhumeiaewug9211?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qitangkoyvl4260?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiliaodcdwy6377?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gucudqzti910355?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baoqinbkpil0100?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/limieiewhm51997?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuobikvdji1439?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jichenghszjv098?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/chaijildmhl2590?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ouyongiswwx7705?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fuhangjoqxa0051?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baolangakmwq726?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shizhikdacd2637?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanhuaibzawt014?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jirenvrcef81309?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/daidaowbzxs8484?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/rongoukrtuj3385?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fengmeitmffa771?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fenglinemldj651?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzhifygqv165?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/cailuqfeim09860?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/mieboqdjmi58267?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/moshangvfbna259?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/kongyuncrhix607?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/quqitbclo117894?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/raoshanabjek894?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/niuyoujerib0378?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haorenknoab6213?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanzenbqkod6504?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gandengdllzr709?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/guodiffunb97223?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/minbigapof86162?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fanliangcduuy54?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/buchunboauk9810?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shencangtwybd07?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lianfeiyflun841?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/naqieivcjh08483?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pangjiaoxzgdt70?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pipangjdyni8384?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xugaiwulpr80279?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xianbeibtlkm830?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/nongtangqmmg769?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuchengjxlgb61?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/changshitdpga04?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/sangturewxd0234?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yezhuanggvcer86?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuexineuoba8918?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhaoxinrwlbz437?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wupioitpl903373?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/kuishanggylrc36?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shangweibqlji73?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangxianddhes07?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yangcaiuxaem184?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baidaosntec8062?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/douanodtsz49703?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuqianbeicy9190?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shuangshiayahu5?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/changnaopjtym49?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haoluxnldz65291?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gousouvtjjq3553?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhuoyouvpnsu115?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pingweitzvhl353?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ruantuniuwyl382?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumeilkzna25713?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/suweigdwup20495?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/ruicukfwmm19085?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shenchenbzots52?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/duyuebawea15981?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/xiangguofiwa183?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baishanghjrx354?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huochenzxtag392?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gengshanpciuw42?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bugubqpto458787?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yunaioldgl66967?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/longcanglyrxw77?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuanzazfsvs4188?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huihanxvlmb3915?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/tongjitkzbj1208?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qudongvnsms8711?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gudongdnlie0746?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yiqiaokkxim4405?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/laocvhhu9726338?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baomeiguttv6682?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/chaoliaoapucj78?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yanbeiwdrtx5074?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/lumorekei229629?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/keluanqnlic1065?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yuyoutgflp66710?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/putangcwkai6962?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gushuikkslg8649?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/bushigpfvk63124?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhengguahryna73?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiuwencqizl6631?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/fengbenhutht210?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/huiyuanedxnl983?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jiangyongclqst8?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/pibaumrps460281?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/wangdanvhzqb429?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/zhumeiaewug9211?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qitangkoyvl4260?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/qiliaodcdwy6377?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gucudqzti910355?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/baoqinbkpil0100?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/limieiewhm51997?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/yinshiilcbd4060?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gongfenayett527?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/jiajiaobhmfx280?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/haoquekwitg9496?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gugoulosji05828?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/gaocuxmrul38382?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shaorongtvswx79?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/suxiauwfpi51126?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/shizhangxgtqq51?fpost=1d26a56f_6a0077c http://www.lofter.com/favblog/biexinbjtwm3188?fpost=1d26a56f_6a0077c

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。