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

时间: 2024-10-18 01:16:49

C++内存分配与对象构造的分离的相关文章

C++ Primer 学习笔记_98_特殊工具与技术 --优化内存分配

特殊工具与技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自动运行合适的构造函数来初始化每个动态分配的类类型对象. new基于每个对象分配内存的事实可能会对某些类强加不可接受的运行时开销,这样的类可能需要使用用户级的类类型对象分配能够更快一些.这样的类使用的通用策略是,预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个新对象. 另外一些类希望按最小尺寸为自己的数据成员分配需要的内存.例如,

C++ Primer 学习笔记_98_非一般工具与技术 -优化内存分配

特殊工具与技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自动运行合适的构造函数来初始化每个动态分配的类类型对象. new基于每个对象分配内存的事实可能会对某些类强加不可接受的运行时开销,这样的类可能需要使用用户级的类类型对象分配能够更快一些.这样的类使用的通用策略是,预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个新对象. 另外一些类希望按最小尺寸为自己的数据成员分配需要的内存.例如,

[C++ primer]优化内存分配

C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自动运行合适的构造函数来初始化每个动态分配的类类型对象. new基于每个对象分配内存的事实可能会对某些类强加不可接受的运行时开销,这样的类可能需要使用用户级的类类型对象分配能够更快一些.这样的类使用的通用策略是,预先分配用于创建新对象的内存,需要时在预先分配的内存中构造每个新对象.另外一些类希望按最小尺寸为自己的数据成员分配需要的内存. 在每种情况下(预先分配内存以保存用户级对象或者保

C++ Primer 学习笔记_98_特殊的工具和技术 --优化内存分配

特殊的工具和技术 --优化内存分配 引言: C++的内存分配是一种类型化操作:new为特定类型分配内存,并在新分配的内存中构造该类型的一个对象.new表达式自己主动执行合适的构造函数来初始化每一个动态分配的类类型对象. new基于每一个对象分配内存的事实可能会对某些类强加不可接受的执行时开销,这种类可能须要使用用户级的类类型对象分配能够更快一些. 这种类使用的通用策略是,预先分配用于创建新对象的内存,须要时在预先分配的内存中构造每一个新对象. 另外一些类希望按最小尺寸为自己的数据成员分配须要的内

Java内存分配、管理小结

 想写这篇总结酝酿了有个来月了,却始终感觉还差点什么东西,一直未敢动笔. 最近两天连夜奋战,重新整理下前面查阅的资料.笔记,还是决定将它写出来. 现在提出几个问题,如果都能熟练回答的大虾,请您飘过.如以往一样,我是小菜,本文自然也是针对小菜阶层的总结. 首先是概念层面的几个问题: Java中运行时内存结构有哪几种? Java中为什么要设计堆栈分离? Java多线程中是如何实现数据共享的? Java反射的基础是什么? 然后是运用层面: 引用类型变量和对象的区别? 什么情况下用局部变量,什么情况下用

java内存分配详细论

P.S. 想写这篇总结酝酿了有个来月了,却始终感觉还差点什么东西,一直未敢动笔. 最近两天连夜奋战,重新整理下前面查阅的资料.笔记,还是决定将它写出来. 现在提出几个问题,如果都能熟练回答的大虾,请您飘过.如以往一样,我是小菜,本文自然也是针对小菜阶层的总结. 首先是概念层面的几个问题: Java中运行时内存结构有哪几种? Java中为什么要设计堆栈分离? Java多线程中是如何实现数据共享的? Java反射的基础是什么? 然后是运用层面: 引用类型变量和对象的区别? 什么情况下用局部变量,什么

C++中的内存分配

C++提供下面两种方法分配和释放未构造的原始内存 (1)allocator 类,它提供可感知类型的内存分配 (2)标准库中的 operator new 和 operator delete,它们分配和释放需要大小的原始未类型化的内存 C++ 还提供不同的方法在原始内存中构造和撤销对象 (1)std::allocator<T>::construct 在未构造内存中初始化对象,std::allocator<T>::destroy 在对象上运行适当的析构函数. (2)定位 new 表达式(

Java内存分配和内存管理

首先是概念层面的几个问题: Java中运行时内存结构有哪几种? Java中为什么要设计堆栈分离? Java多线程中是如何实现数据共享的? Java反射的基础是什么? 然后是运用层面: 引用类型变量和对象的区别? 什么情况下用局部变量,什么情况下用成员变量? 数组如何初始化?声明一个数组的过程中,如何分配内存? 声明基本类型数组和声明引用类型的数组,初始化时,内存分配机制有什么区? 在什么情况下,我们的方法设计为静态化,为什么?(上次胡老师问文奇,问的哑口无言,当时想回答,却老感觉表述不清楚,这里

垃圾收集器与内存分配策略

①对于java虚拟机来说,垃圾收集器主要关注的内存区域是 堆和方法区. ②垃圾收集器就是要收集那些已经“死了”的对象.如果判断一个对象是否存活? 对象引用计数法 对象引用增加一个,那么相应的计数器加1,否则,减1. 优点:实现简单 缺点:不能处理对象间的循环引用.a引用b,b同时引用a. 可达性分析 如果节点到root节点可达,则证明是存活的:否则,已死.所以对于下图的o5,o6,o7虽然他们是循环引用的,但是到root节点无可达,所以已死可清除. ③垃圾回收器对于不同类型引用的回收规则 强引用