| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> C++ nei cun guan li xiang jie

C++ nei cun guan li xiang jie

2008-06-01 02:08:15 编辑來源:互联网 评论
 
 
 
本文为【C++内存管理详解】的拼音翻译版
  1. dui ying de new he delete yao cai yong xiang tong de xing shi xia mian de yu ju you shen me cuo ?
  string *stringarray = new string[100];
  ...
  delete stringarray;
  yi qie hao xiang dou jing ran you xu —— yi ge new dui ying zhe yi ge delete—— ran er que yin cang zhe hen da de cuo wu : cheng xu de yun xing qing kuang jiang shi bu ke cai ce de 。 zhi shao ,stringarray zhi xiang de 100 ge string dui xiang zhong de 99 ge bu hui bei zheng que di cui hui , yin wei ta men de xi gou han shu yong yuan bu hui bei tiao yong 。
  yong new de shi hou hui fa sheng liang jian shi 。 shou xian , nei cun bei fen pei ( tong guo operator new han shu , xiang jian tiao kuan 7-10 he tiao kuan m8), ran hou , wei bei fen pei de nei cun tiao yong yi ge huo duo ge gou zao han shu 。 yong delete de shi hou , ye you liang jian shi fa sheng : shou xian , wei jiang bei shi fang de nei cun tiao yong yi ge huo duo ge xi gou han shu , ran hou , shi fang nei cun ( tong guo operator delete han shu , xiang jian tiao kuan 8 he m8)。 dui yu delete lai shuo hui you zhe yang yi ge chong yao de wen ti : nei cun zhong you duo shao ge dui xiang yao bei shan chu ? da an jue ding le jiang you duo shao ge xi gou han shu hui bei tiao yong 。
  zhe ge wen ti jian dan lai shuo jiu shi : yao bei shan chu de zhi zhen zhi xiang de shi dan ge dui xiang ne , huan shi dui xiang shu zu ? zhe zhi you ni lai gao su delete。 jia ru ni zai yong delete shi mei yong kuo hao ,delete jiu hui ren wei zhi xiang de shi dan ge dui xiang , fou ze , ta jiu hui ren wei zhi xiang de shi yi ge shu zu :
  string *stringptr1 = new string;
  string *stringptr2 = new string[100];
  ...
  delete stringptr1;// shan chu yi ge dui xiang
  delete [] stringptr2;// shan chu dui xiang shu zu
  jia ru ni zai stringptr1 qian jia le "[]" hui zen yang ne ? da an shi : na jiang shi bu ke cai ce de ; jia ru ni mei zai stringptr2 qian mei jia shang "[]" you hui zen yang ne ? da an ye shi : bu ke cai ce 。 er qie dui yu xiang int zhe yang de gu ding lei xing lai shuo , jie guo ye shi bu ke cai ce de , ji shi zhe yang de lei xing mei you xi gou han shu 。 suo yi , jie jue zhe lei wen ti de gui ze hen jian dan : jia ru ni tiao yong new shi yong le [], tiao yong delete shi ye yao yong []。 jia ru tiao yong new shi mei you yong [], na tiao yong delete shi ye bu yao yong []。
  zai xie yi ge bao han zhi zhen shu ju cheng yuan , bing qie ti gong duo ge gou zao han shu de lei shi , lao ji zhe yi gui ze you qi chong yao 。 yin wei zhe yang de hua , ni jiu bi xu zai suo you chu shi hua zhi zhen cheng yuan de gou zao han shu li cai yong xiang tong de new de xing shi 。 fou ze , xi gou han shu li jiang cai yong shen me xing shi de delete ne ? guan yu zhe yi hua ti de jin yi bu chan shu , shen jian tiao kuan 11。
  zhe ge gui ze dui xi huan yong typedef de ren lai shuo ye hen chong yao , yin wei xie typedef de cheng xu yuan bi xu gao su bie ren , yong new chuang jian le yi ge typedef ding yi de lei xing de dui xiang hou , gai yong shen me xing shi de delete lai shan chu 。 ju li ru xia :
  typedef string addresslines[4]; // yi ge ren de di zhi , gong 4 xing , mei xing yi ge string
  // yin wei addresslines shi ge shu zu , shi yong new:
  string *pal = new addresslines; // zhu chong "new addresslines" fan hui string*, he
  // "new string[4]" fan hui de yi yang
  delete shi bi xu yi shu zu xing shi yu zhi dui ying :
  delete pal;// cuo wu !
  delete [] pal;// zheng que
  wei le bi mian hun luan , zui hao du jue dui shu zu lei xing yong typedefs。 zhe qi shi hen qing yi , yin wei biao zhun c++ ku ( jian tiao kuan 49) bao han you stirng he vector mo ban , shi yong ta men jiang hui shi dui shu zu de xu qiu jian shao dao ji hu ling 。 ju li lai shuo ,addresslines ke yi ding yi wei yi ge zi fu chuan (string) de xiang liang (vector), ji addresslines ke ding yi wei vector lei xing 。
  2. xi gou han shu li dui zhi zhen cheng yuan tiao yong delete
  da duo shu qing kuang xia , zhi xing dong tai nei cun fen pei de de lei dou zai gou zao han shu li yong new fen pei nei cun , ran hou zai xi gou han shu li yong delete shi fang nei cun 。 zui chu xie zhe ge lei de shi hou dang ran bu nan zuo , ni hui ji de zui hou dui zai suo you gou zao han shu li fen pei le nei cun de suo you cheng yuan shi yong delete。
  ran er , zhe ge lei jing guo wei hu 、 sheng ji hou , qing kuang jiu hui bian de kun nan le , yin wei dui lei de dai ma jin xing xiu gai de cheng xu yuan bu yi ding jiu shi zui zao xie zhe ge lei de ren 。 er zeng jia yi ge zhi zhen cheng yuan yi wei zhe ji hu dou yao jin xing xia mian de gong zuo :
  · zai mei ge gou zao han shu li dui zhi zhen jin xing chu shi hua 。 dui yu yi xie gou zao han shu , jia ru mei you nei cun yao fen pei gei zhi zhen de hua , zhi zhen yao bei chu shi hua wei 0( ji kong zhi zhen )。
  · shan chu xian you de nei cun , tong guo fu zhi cao zuo fu fen pei gei zhi zhen xin de nei cun 。
  · zai xi gou han shu li shan chu zhi zhen 。
  jia ru zai gou zao han shu li wang le chu shi hua mou ge zhi zhen , huo zhe zai fu zhi cao zuo de guo cheng zhong wang le chu li ta , wen ti hui chu xian de hen kuai , hen ming xian , suo yi zai shi jian zhong zhe liang ge wen ti bu hui na me zhe mo ni 。 dan shi , jia ru zai xi gou han shu li mei you shan chu zhi zhen , ta bu hui biao xian chu hen ming xian de wai bu zheng zhuang 。 xiang fan , ta ke neng zhi shi biao xian wei yi dian wei xiao de nei cun xie lu , bing qie bu duan zeng chang , zui hou tun shi le ni de di zhi kong jian , dao zhi cheng xu yao zhe 。 yin wei zhe zhong qing kuang jing chang bu na me yin ren zhu chong , suo yi mei zeng jia yi ge zhi zhen cheng yuan dao lei li shi yi ding yao ji qing chu 。
  ling wai , shan chu kong zhi zhen shi an quan de ( yin wei ta shen me ye mei zuo )。 suo yi , zai xie gou zao han shu , fu zhi cao zuo fu , huo qi ta cheng yuan han shu shi , lei de mei ge zhi zhen cheng yuan yao me zhi xiang you xiao de nei cun , yao me jiu zhi xiang kong , na zai ni de xi gou han shu li ni jiu ke yi zhi yong jian dan di delete diao ta men , er bu yong dan xin ta men shi bu shi bei new guo 。
  dang ran dui ben tiao kuan de shi yong ye bu yao jue dui 。 li ru , ni dang ran bu hui yong delete qu shan chu yi ge mei you yong new lai chu shi hua de zhi zhen , er qie , jiu xiang yong zhi neng zhi zhen dui xiang shi bu yong lao ni qu shan chu yi yang , ni ye yong yuan bu hui qu shan chu yi ge chuan di gei ni de zhi zhen 。 huan ju hua shuo , chu fei lei cheng yuan zui chu yong le new, fou ze shi bu yong zai xi gou han shu li yong delete de 。
  shuo dao zhi neng zhi zhen , zhe li jie shao yi zhong bi mian bi xu shan chu zhi zhen cheng yuan de fang fa , ji ba zhe xie cheng yuan yong zhi neng zhi zhen dui xiang lai dai ti , bi ru c++ biao zhun ku li de auto_ptr。 xiang zhi dao ta shi ru he gong zuo de , kan kan tiao kuan m9 he m10。
  3. yu xian yu bei hao nei cun bu gou de qing kuang
  operator new zai wu fa wan cheng nei cun fen pei qing qiu shi hui pao chu yi chang ( yi qian de zuo fa yi ban shi fan hui 0, yi xie jiu yi dian de bian yi qi huan zhe me zuo 。 ni yuan yi de hua ye ke yi ba ni de bian yi qi she zhi cheng zhe yang 。 guan yu zhe ge hua ti wo jiang tui chi dao ben tiao kuan de jie yi chu tao lun )。 da jia dou zhi dao , chu li nei cun bu gou suo chan sheng de yi chang zhen ke yi suan de shang shi ge dao de shang de xing wei , dan shi ji zuo qi lai you hui xiang dao jia zai bo zi shang na yang tong ku 。 suo yi , ni you shi hui bu qu guan ta , ye xu yi zhi mei qu guan ta 。 dan ni xin li yi ding huan shi shen shen di yin cang zhe yi zhong zui e gan : wan yi new zhen de chan sheng le yi chang zen me ban ?
  ni hui hen zi ran di xiang dao chu li zhe zhong qing kuang de yi zhong fang fa , ji hui dao yi qian de lao lu shang qu , shi yong yu chu li 。 li ru ,c de yi zhong chang yong de zuo fa shi , ding yi yi ge lei xing wu guan de hong lai fen pei nei cun bing jian cha fen pei shi fou cheng gong 。 dui yu c++ lai shuo , zhe ge hong kan qi lai ke neng xiang zhe yang :
  
  
  #define new(ptr, type)
  try { (ptr) = new type; }
  catch (std::bad_alloc&) { assert(0); }
  (“ man !std::bad_alloc shi zuo shen me de ?” ni hui wen 。bad_alloc shi operator new bu neng man zu nei cun fen pei qing qiu shi pao chu de yi chang lei xing ,std shi bad_alloc suo zai de ming zi kong jian ( jian tiao kuan 28) de ming cheng 。“ hao !” ni hui ji xu wen ,“assert you you shen me yong ?” jia ru ni kan kan biao zhun c tou wen jian ( huo yu ta xiang deng jia de yong dao le ming zi kong jian de ban ben , jian tiao kuan 49), jiu hui fa xian assert shi ge hong 。 zhe ge hong jian cha chuan gei ta de biao da shi shi fou fei ling , jia ru bu shi fei ling zhi , jiu hui fa chu yi tiao chu cuo xin xi bing tiao yong abort。assert zhi shi zai mei ding yi biao zhun hong ndebug de shi hou , ji zai tiao shi zhuang tai xia cai zhe me zuo 。 zai chan pin fa bu zhuang tai xia , ji ding yi le ndebug de shi hou ,assert shen me ye bu zuo , xiang dang yu yi tiao kong yu ju 。 suo yi ni zhi neng zai tiao shi shi cai neng jian cha duan yan (assertion))。
  new hong bu dan you zhe shang mian suo shuo de tong bing , ji yong assert qu jian cha ke neng fa sheng zai yi fa bu cheng xu li de zhuang tai ( ran er ren he shi hou dou ke neng fa sheng nei cun bu gou de qing kuang ), tong shi , ta huan zai c ++ li you ling wai yi ge que xian : ta mei you kao lv dao new you ge zhong ge yang de shi yong fang shi 。 li ru , xiang chuang jian lei xing t dui xiang , yi ban you san zhong chang jian de yu fa xing shi , ni bi xu dui mei zhong xing shi ke neng chan sheng de yi chang dou yao jin xing chu li :
  new t;
  new t(constrUCtor arguments);
  new t[size];
  zhe li dui wen ti da da jin xing le jian hua , yin wei you ren huan hui zi ding yi ( chong zai )operator new, suo yi cheng xu li hui bao han ren yi ge shi yong new de yu fa xing shi 。
  na me , zen me ban ? jia ru xiang yong yi ge hen jian dan de chu cuo chu li fang fa , ke yi zhe me zuo : dang nei cun fen pei qing qiu bu neng man zu shi , tiao yong ni yu xian zhi ding de yi ge chu cuo chu li han shu 。 zhe ge fang fa ji yu yi ge chang gui , ji dang operator new bu neng man zu qing qiu shi , hui zai pao chu yi chang zhi qian tiao yong ke hu zhi ding de yi ge chu cuo chu li han shu —— yi ban cheng wei new-handler han shu 。(operator new shi ji gong zuo qi lai yao fu duo yi xie , xiang jian tiao kuan 8)
  zhi ding chu cuo chu li han shu shi yao yong dao set_new_handler han shu , ta zai tou wen jian li da zhi shi xiang xia mian zhe yang ding yi de :
  typedef void (*new_handler)();
  new_handler set_new_handler(new_handler p) throw();
  ke yi kan dao ,new_handler shi yi ge zi ding yi de han shu zhi zhen lei xing , ta zhi xiang yi ge mei you shu ru shen shu ye mei you fan hui zhi de han shu 。set_new_handler ze shi yi ge shu ru bing fan hui new_handler lei xing de han shu 。
  set_new_handler de shu ru shen shu shi operator new fen pei nei cun shi bai shi yao tiao yong de chu cuo chu li han shu de zhi zhen , fan hui zhi shi set_new_handler mei tiao yong zhi qian jiu yi jing zai qi zuo yong de jiu de chu cuo chu li han shu de zhi zhen 。
  ke yi xiang xia mian zhe yang shi yong set_new_handler:
  // function to call if operator new can't allocate enough memory
  void nomorememory()
  {
  cerr << "unable to satisfy request for memory
  ";
  abort();
  }
  int main()
  {
  set_new_handler(nomorememory);
  int *pbigdataarray = new int[100000000];
  ...
  }
  jia ru operator new bu neng wei 100,000,000 ge zheng shu fen pei kong jian ,nomorememory jiang hui bei tiao yong , cheng xu fa chu yi tiao chu cuo xin xi hou zhong zhi 。 zhe jiu bi jian dan di rang xi tong nei gai chan sheng cuo wu xin xi lai jie shu cheng xu yao hao 。( shun bian kao lv yi xia , jia ru cerr zai xie cuo wu xin xi de guo cheng zhong yao dong tai fen pei nei cun , na jiang hui fa sheng shen me ...)
  operator new bu neng man zu nei cun fen pei qing qiu shi ,new-handler han shu bu zhi tiao yong yi ci , er shi bu duan chong fu , zhi zhi zhao dao zu gou de nei cun 。 shi xian chong fu tiao yong de dai ma zai tiao kuan 8 li ke yi kan dao , zhe li wo yong miao shu xing de de yu yan lai shuo ming : yi ge she ji de hao de new-handler han shu bi xu shi xian xia mian gong neng zhong de yi zhong 。
  · chan sheng geng duo de ke yong nei cun 。 zhe jiang shi operator new xia yi ci fen pei nei cun de chang shi you ke neng huo de cheng gong 。 shi shi zhe yi ce lue de yi ge fang fa shi : zai cheng xu qi dong shi fen pei yi ge da de nei cun kuai , ran hou zai di yi ci tiao yong new-handler shi shi fang 。 shi fang shi ban sui zhe yi xie dui yong hu de jing gao xin xi , ru nei cun shu liang tai shao , xia ci qing qiu ke neng hui shi bai , chu fei you you geng duo de ke yong kong jian 。
  · an zhuang ling yi ge bu tong de new-handler han shu 。 jia ru dang qian de new-handler han shu bu neng chan sheng geng duo de ke yong nei cun , ke neng ta hui zhi dao ling yi ge new- handler han shu ke yi ti gong geng duo de zi yuan 。 zhe yang de hua , dang qian de new-handler ke yi an zhuang ling yi ge new-handler lai qu dai ta ( tong guo tiao yong set_new_handler)。 xia yi ci operator new tiao yong new-handler shi , hui shi yong zui jin an zhuang de na ge 。( zhe yi ce lue de ling yi ge bian tong ban fa shi rang new-handler ke yi gai bian ta zi ji de yun xing xing wei , na me xia ci tiao yong shi , ta jiang zuo bu tong de shi 。 fang fa shi shi new-handler ke yi xiu gai na xie ying xiang ta zi shen xing wei de jing tai huo quan ju shu ju 。)
  · xie chu new-handler。 ye jiu shi chuan di kong zhi zhen gei set_new_handler。 mei you an zhuang new-handler,operator new fen pei nei cun bu cheng gong shi jiu hui pao chu yi ge biao zhun de std::bad_alloc lei xing de yi chang 。
  · pao chu std::bad_alloc huo cong std::bad_alloc ji xu de qi ta lei xing de yi chang 。 zhe yang de yi chang bu hui bei operator new bu zhuo , suo yi ta men hui bei song dao zui chu jin xing nei cun qing qiu de di fang 。( pao chu bie de bu tong lei xing de yi chang hui wei fan operator new yi chang gui fan 。 gui fan zhong de que sheng xing wei shi tiao yong abort, suo yi new-handler yao pao chu yi ge yi chang shi , yi ding yao que xin ta shi cong std::bad_alloc ji xu lai de 。 xiang geng duo di le jie yi chang gui fan , shen jian tiao kuan m14。)
  · mei you fan hui 。 dian xing zuo fa shi tiao yong abort huo exit。abort/exit ke yi zai biao zhun c ku zhong zhao dao ( huan you biao zhun c++ ku , shen jian tiao kuan 49)。
  shang mian de xuan ze gei le ni shi xian new-handler han shu ji da de ling huo xing 。
  chu li nei cun fen pei shi bai de qing kuang shi cai qu shen me fang fa , qu jue yu yao fen pei de dui xiang de lei :
  class x {
  public:
  static void
  
   outofmemory();
  ...
  };
  class y {
  public:
  static void outofmemory();
  ...
  };
  x* p1 = new x; // ruo fen pei cheng gong , tiao yong x::outofmemory
  y* p2 = new y; // ruo fen pei bu cheng gong , tiao yong y::outofmemory
  c++ bu zhi chi zhuan men zhen dui yu lei de new-handler han shu , er qie ye bu xu yao 。 ni ke yi zi ji lai shi xian ta , zhi yao zai mei ge lei zhong ti gong zi ji ban ben de set_new_handler he operator new。 lei de set_new_handler ke yi wei lei zhi ding new-handler( jiu xiang biao zhun de set_new_handler zhi ding quan ju new-handler yi yang )。 lei de operator new ze bao zheng wei lei de dui xiang fen pei nei cun shi yong lei de new-handler qu dai quan ju new-handler。
  jia she chu li lei x nei cun fen pei shi bai de qing kuang 。 yin wei operator new dui lei xing x de dui xiang fen pei nei cun shi bai shi , mei ci dou bi xu tiao yong chu cuo chu li han shu , suo yi yao zai lei li sheng ming yi ge new_handler lei xing de jing tai cheng yuan 。 na me lei x kan qi lai hui xiang zhe yang :
  class x {
  public:
  static new_handler set_new_handler(new_handler p);
  static void * operator new(size_t size);
  private:
  static new_handler currenthandler;
  };
  lei de jing tai cheng yuan bi xu zai lei wai ding yi 。 yin wei xiang jie yong jing tai dui xiang de que sheng chu shi hua zhi 0, suo yi ding yi x::currenthandler shi mei you qu chu shi hua 。
  new_handler x::currenthandler; // que sheng she zhi currenthandler wei 0( ji null)
  lei x zhong de set_new_handler han shu hui bao cun chuan gei ta de ren he zhi zhen , bing fan hui zai tiao yong ta zhi qian suo bao cun de ren he zhi zhen 。 zhe zheng shi biao zhun ban ben de set_new_handler suo zuo de :
  new_handler x::set_new_handler(new_handler p)
  {
  new_handler oldhandler = currenthandler;
  currenthandler = p;
  return oldhandler;
  }
  zui hou kan kan x de operator new suo zuo de :
  1. tiao yong biao zhun set_new_handler han shu , shu ru shen shu wei x de chu cuo chu li han shu 。 zhe shi de x de new-handler han shu cheng wei quan ju new-handler han shu 。 zhu chong xia mian de dai ma zhong , yong le "::" fu hao xian shi di yin yong std kong jian ( biao zhun set_new_handler han shu jiu cun zai yu std kong jian )。
  2. tiao yong quan ju operator new fen pei nei cun 。 jia ru di yi ci fen pei shi bai , quan ju operator new hui tiao yong x de new-handler, yin wei ta gang gang ( jian 1.) bei an zhuang cheng wei quan ju new-handler。 jia ru quan ju operator new zui zhong wei neng fen pei dao nei cun , ta pao chu std::bad_alloc yi chang ,x de operator new hui bu zhuo dao ta 。x de operator new ran hou hui fu zui chu bei qu dai de quan ju new-handler han shu , zui hou yi pao chu yi chang fan hui 。
  3. jia she quan ju operator new wei lei xing x de dui xiang fen pei nei cun cheng gong ,, x de operator new hui zai ci tiao yong biao zhun set_new_handler lai hui fu zui chu de quan ju chu cuo chu li han shu 。 zui hou fan hui fen pei cheng gong de nei cun de zhi zhen 。
  c++ shi zhe me zuo de :
  void * x::operator new(size_t size)
  {
  new_handler globalhandler = // an zhuang x de new_handler
  std::set_new_handler(currenthandler);
  void *memory;
  try { // chang shi fen pei nei cun
  memory = ::operator new(size);
  }
  catch (std::bad_alloc&) { // hui fu jiu de new_handler
  std::set_new_handler(globalhandler);
  throw; // pao chu yi chang
  }
  std::set_new_handler(globalhandler); // hui fu jiu de new_handler
  return memory;
  }
  jia ru ni dui shang mian chong fu tiao yong std::set_new_handler kan bu shun yan , ke yi shen jian tiao kuan m9 lai chu qu ta men 。
  shi yong lei x de nei cun fen pei chu li gong neng shi da zhi ru xia :
  void nomorememory();// x de dui xiang fen pei nei cun shi bai shi tiao yong de new_handler han shu de sheng ming
  x::set_new_handler(nomorememory);
  // ba nomorememory she zhi wei x de
  // new-handling han shu
  x *px1 = new x;
  // ru nei cun fen pei shi bai ,
  // tiao yong nomorememory
  string *ps = new string;
  // ru nei cun fen pei shi bai , tiao yong quan ju new-handling han shu
  x::set_new_handler(0);
  // she x de new-handling han shu wei kong
  x *px2 = new x;
  // ru nei cun fen pei shi bai , li ji pao chu yi chang
  // ( lei x mei you new-handling han shu )
  ni hui zhu chong dao , chu li yi shang lei si qing kuang , jia ru bu kao lv lei de hua , shi xian dai ma shi yi yang de , zhe jiu hen zi ran di xiang dao zai bie de di fang ye neng chong yong ta men 。 zheng ru tiao kuan 41 suo shuo ming de , ji xu he mo ban ke yi yong lai she ji ke chong yong dai ma 。 zai zhe li , wo men ba liang zhong fang fa jie he qi lai shi yong , cong er man zu le ni de yao qiu 。
  ni zhi yao chuang jian yi ge “ hun he feng ge ”(mixin-style) de ji lei , zhe zhong ji lei da ying zi lei ji xu ta mou yi te ding de gong neng —— zhe li zhi de shi jian li yi ge lei de new-handler de gong neng 。 zhi suo yi she ji yi ge ji lei , shi wei le rang suo you de zi lei ke yi ji xu set_new_handler he operator new gong neng , er she ji mo ban shi wei le shi mei ge zi lei you bu tong de currenthandler shu ju cheng yuan 。 zhe ting qi lai hen fu duo , bu guo ni hui kan dao dai ma qi shi hen shu xi 。 qu bie zhi bu guo shi ta xian zai ke yi bei ren he lei chong yong le 。
  template // ti gong lei set_new_handler zhi chi de
  class newhandlersupport { // hun he feng ge ” de ji lei
  public:
  static new_handler set_new_handler(new_handler p);
  
   static void * operator new(size_t size);
  private:
  static new_handler currenthandler;
  };
  template
  new_handler newhandlersupport::set_new_handler(new_handler p)
  {
  new_handler oldhandler = currenthandler;
  currenthandler = p;
  return oldhandler;
  }
  template
  void * newhandlersupport::operator new(size_t size)
  {
  new_handler globalhandler =
  std::set_new_handler(currenthandler);
  void *memory;
  try {
  memory = ::operator new(size);
  }
  catch (std::bad_alloc&) {
  std::set_new_handler(globalhandler);
  throw;
  }
  std::set_new_handler(globalhandler);
  return memory;
  }
  // this sets each currenthandler to 0
  template
  new_handler newhandlersupport::currenthandler;
  you le zhe ge mo ban lei , dui lei x jia shang set_new_handler gong neng jiu hen jian dan le : zhi yao rang x cong newhandlersupport ji xu :
  // note inheritance from mixin base class template. (see
  // my article on counting objects for information on why
  // private inheritance might be preferable here.)
  class x: public newhandlersupport {
  ... // as before, but no declarations for
  }; // set_new_handler or operator new
  shi yong x de shi hou yi ran bu yong li hui ta mu hou zai zuo xie shen me ; lao dai ma yi ran gong zuo 。 zhe hen hao ! na xie ni chang bu qu li hui de dong xi wang wang shi zui ke xin lai de 。
  shi yong set_new_handler shi chu li nei cun bu gou qing kuang xia yi zhong fang bian , jian dan de fang fa 。 zhe bi ba mei ge new dou bao zhuang zai try mo kuai li dang ran hao duo le 。 er qie , newhandlersupport zhe yang de mo ban shi de xiang ren he lei zeng jia yi ge te ding de new-handler bian de geng jian dan 。“ hun he feng ge ” de ji xu bu ke bi mian di jiang hua ti yin ru dao duo ji xu shang qu , zai zhuan dao zhe ge hua ti qian , ni yi ding yao xian yue du tiao kuan 43。
  1993 nian qian ,c++ yi zhi yao qiu zai nei cun fen pei shi bai shi operator new yao fan hui 0, xian zai ze shi yao qiu operator new pao chu std::bad_alloc yi chang 。 hen duo c++ cheng xu shi zai bian yi qi kai shi zhi chi xin gui fan qian xie de 。c++ biao zhun wei yuan hui bu xiang fang qi na xie yi you de zun xun fan hui 0 gui fan de dai ma , suo yi ta men ti gong le ling wai xing shi de operator new( yi ji operator new[]—— jian tiao kuan 8) yi ji xu ti gong fan hui 0 gong neng 。 zhe xie xing shi bei cheng wei “ wu pao chu ”, yin wei ta men mei yong guo yi ge throw, er shi zai shi yong new de ru kou dian cai yong le nothrow dui xiang :
  class widget { ... };
  widget *pw1 = new widget;// fen pei shi bai pao chu std::bad_alloc if
  if (pw1 == 0) ... // zhe ge jian cha yi ding shi bai
  widget *pw2 = new (nothrow) widget; // ruo fen pei shi bai fan hui 0
  if (pw2 == 0) ... // zhe ge jian cha ke neng hui cheng gong
  bu guan shi yong “ zheng gui ”( ji pao chu yi chang ) xing shi de new huan shi “ wu pao chu ” xing shi de new, chong yao de shi ni bi xu wei nei cun fen pei shi bai zuo hao yu bei 。 zui jian dan de fang fa shi shi yong set_new_handler, yin wei ta dui liang zhong xing shi dou you yong 。
  4. xie operator new he operator delete shi yao zun xun chang gui
  zi ji chong xie operator new shi ( tiao kuan 10 jie shi le wei shen me you shi yao chong xie ta ), hen chong yao de yi dian shi han shu ti gong de xing wei yao he xi tong que sheng de operator new yi zhi 。 shi ji zuo qi lai ye jiu shi : yao you zheng que de fan hui zhi ; ke yong nei cun bu gou shi yao tiao yong chu cuo chu li han shu ( jian tiao kuan 7); chu li hao 0 zi jie nei cun qing qiu de qing kuang 。 ci wai , huan yao bi mian bu xiao xin yin cang le biao zhun xing shi de new, bu guo zhe shi tiao kuan 9 de hua ti 。
  you guan fan hui zhi de bu fen hen jian dan 。 jia ru nei cun fen pei qing qiu cheng gong , jiu fan hui zhi xiang nei cun de zhi zhen ; jia ru shi bai , ze zun xun tiao kuan 7 de gui ding pao chu yi ge std::bad_alloc lei xing de yi chang 。
  dan shi qing ye bu shi na me jian dan 。 yin wei operator new shi ji shang hui bu zhi yi ci di chang shi zhe qu fen pei nei cun , ta yao zai mei ci shi bai hou tiao yong chu cuo chu li han shu , huan qi wang chu cuo chu li han shu neng xiang ban fa shi fang bie chu de nei cun 。 zhi you zai zhi xiang chu cuo chu li han shu de zhi zhen wei kong de qing kuang xia ,operator new cai pao chu yi chang 。
  ling wai ,c++ biao zhun yao qiu , ji shi zai qing qiu fen pei 0 zi jie nei cun shi ,operator new ye yao fan hui yi ge he fa zhi zhen 。( shi ji shang , zhe ge ting qi lai guai guai de yao qiu que shi gei c++ yu yan qi ta di fang dai lai le jian bian )
  zhe yang , fei lei cheng yuan xing shi de operator new de wei dai ma kan qi lai hui xiang xia mian zhe yang :
  void * operator new(size_t size) // operator new huan ke neng you qi ta shen shu
  {
  if (size == 0) { // chu li 0 zi jie qing qiu shi ,
  size = 1; // ba ta dang zuo 1 ge zi jie qing qiu lai chu li
  }
  while (1) {
  fen pei size zi jie nei cun ;
  if ( fen pei cheng gong )
  return ( zhi xiang nei cun de zhi zhen );
  // fen pei bu cheng gong , zhao chu dang qian chu cuo chu li han shu
  new_handler globalhandler = set_new_handler(0);
  set_new_handler(globalhandler);
  if (globalhandler) (*globalhandler)();
  else throw std::bad_alloc();
  }
  }
  chu li ling zi jie qing qiu de ji qiao zai yu ba ta zuo wei qing qiu yi ge zi jie lai chu li 。 zhe kan qi lai ye hen guai , dan jian dan , he fa , you xiao 。 er qie , ni you hui duo jiu peng dao yi ci ling zi jie qing qiu de qing kuang ne ?
  ni you hui xi qi shang mian de wei dai ma zhong wei shen me ba chu cuo chu li han shu zhi wei 0 hou you li ji hui fu 。 zhe shi yin wei mei you ban fa ke yi zhi jie de dao chu cuo chu li han shu de zhi zhen , suo yi bi xu tong guo tiao yong set_new_handler lai zhao dao 。 ban fa hen ben dan ye you xiao 。
  
  
  tiao kuan 7 ti dao operator new nei bu bao han yi ge wu xian xun huan , shang mian de dai ma qing chu di shuo ming le zhe yi dian ——while (1) jiang dao zhi wu xian xun huan 。 tiao chu xun huan de wei yi ban fa shi nei cun fen pei cheng gong huo chu cuo chu li han shu wan cheng le tiao kuan 7 suo miao shu de shi jian zhong de yi zhong : de dao le geng duo de ke yong nei cun ; an zhuang le yi ge xin de new -handler( chu cuo chu li han shu ); xie chu le new-handler; pao chu le yi ge std::bad_alloc huo qi pai sheng lei xing de yi chang ; huo zhe fan hui shi bai 。 xian zai ming bai le wei shen me new-handler bi xu zuo zhe xie gong zuo zhong de yi jian 。 jia ru bu zuo ,operator new li mian de xun huan jiu bu hui jie shu 。
  hen duo ren mei you shu xi dao de yi dian shi operator new jing chang hui bei zi lei ji xu 。 zhe hui dao zhi mou xie fu duo xing 。 shang mian de wei dai ma zhong , han shu hui qu fen pei size zi jie de nei cun ( chu fei size wei 0)。size hen chong yao , yin wei ta shi chuan di gei han shu de shen shu 。 dan shi da duo shu zhen dui lei suo xie de operator new( bao kuo tiao kuan 10 zhong de na zhong ) dou shi zhi wei te ding de lei she ji de , bu shi wei suo you de lei , ye bu shi wei ta suo you de zi lei she ji de 。 zhe yi wei zhe , dui yu yi ge lei x de operator new lai shuo , han shu nei bu de xing wei zai she ji dao dui xiang de da xiao shi , dou shi jing que de sizeof(x): bu hui da ye bu hui xiao 。 dan you yu cun zai ji xu , ji lei zhong de operator new ke neng hui bei tiao yong qu wei yi ge zi lei dui xiang fen pei nei cun :
  class base {
  public:
  static void * operator new(size_t size);
  ...
  };
  class derived: public base // derived lei mei you sheng ming operator new
  { ... }; //
  derived *p = new derived; // tiao yong base::operator new
  jia ru base lei de operator new bu xiang fei gong fu zhuan men qu chu li zhe zhong qing kuang —— zhe zhong qing kuang chu xian de ke neng xing bu da —— na zui jian dan de ban fa shi ba zhe ge “ cuo wu ” shu liang de nei cun fen pei qing qiu zhuan gei biao zhun operator new lai chu li , xiang xia mian zhe yang :
  void * base::operator new(size_t size)
  {
  if (size != sizeof(base)) // jia ru shu liang “ cuo wu ”, rang biao zhun operator new
  return ::operator new(size); // qu chu li zhe ge qing qiu
  //
  ... // fou ze chu li zhe ge qing qiu
  }
  “ ting !” wo ting jian ni zai jiao ,“ ni wang le jian cha yi zhong sui ran bu he li dan shi you ke neng chu xian de yi zhong qing kuang ——size you ke neng wei ling !” shi de , wo mei jian cha , dan bai tuo xia ci zai jiao chu sheng de shi hou bu yao zhe me wen zhou zhou de 。:) dan shi ji shang jian cha huan shi zuo le , zhi bu guo rong he dao size != sizeof(base) yu ju zhong le 。c++ biao zhun hen guai yi , qi zhong zhi yi jiu shi gui ding suo yi du li de (freestanding) lei de da xiao dou shi fei ling zhi 。 suo yi sizeof(base) yong yuan bu ke neng shi ling ( ji shi base lei mei you cheng yuan ), jia ru size wei ling , qing qiu hui zhuan dao ::operator new, you ta lai yi yi zhong he li de fang shi dui qing qiu jin xing chu li 。( you qu de shi , jia ru base bu shi du li de lei ,sizeof(base) you ke neng shi ling , ju ti shuo ming shen jian "my article on counting objects")。
  jia ru xiang kong zhi ji yu lei de shu zu de nei cun fen pei , bi xu shi xian operator new de shu zu xing shi ——operator new[]( zhe ge han shu chang bei cheng wei “ shu zu new”, yin wei xiang bu chu "operator new[]") gai zen me fa yin )。 xie operator new[] shi , yao ji zhu ni mian dui de shi “ yuan shi ” nei cun , bu neng dui shu zu li huan bu cun zai de dui xiang jin xing ren he cao zuo 。 shi ji shang , ni shen zhi huan bu zhi dao shu zu li you duo shao ge dui xiang , yin wei bu zhi dao mei ge dui xiang you duo da 。 ji lei de operator new[] hui tong guo ji xu de fang shi bei yong lai wei zi lei dui xiang de shu zu fen pei nei cun , er zi lei dui xiang wang wang bi ji lei yao da 。 suo yi , bu neng xiang dang ran ren wei base::operator new[] li de mei ge dui xiang de da xiao dou shi sizeof(base), ye jiu shi shuo , shu zu li dui xiang de shu liang bu yi ding jiu shi ( qing qiu zi jie shu )/sizeof(base)。 guan yu operator new[] de ju ti jie shao shen jian tiao kuan m8。
  chong xie operator new( he operator new[]) shi suo you yao zun xun de chang gui jiu zhe xie 。 dui yu operator delete( yi ji ta de huo ban operator delete[]), qing kuang geng jian dan 。 suo yao ji zhu de zhi shi ,c++ bao zheng shan chu kong zhi zhen yong yuan shi an quan de , suo yi ni yao chong fen di ying yong zhe yi bao zheng 。 xia mian shi fei lei cheng yuan xing shi de operator delete de wei dai ma :
  void operator delete(void *rawmemory)
  {
  if (rawmemory == 0) return; file:// ru / guo zhi zhen wei kong , fan hui
  //
  shi fang rawmemory zhi xiang de nei cun ;
  return;
  }
  zhe ge han shu de lei cheng yuan ban ben ye jian dan , zhi shi huan bi xu jian cha bei shan chu de dui xiang de da xiao 。 jia she lei de operator new jiang “ cuo wu ” da xiao de fen pei qing qiu zhuan gei ::operator new, na me ye bi xu jiang “ cuo wu ” da xiao de shan chu qing qiu zhuan gei ::operator delete:
  class base { // he qian mian yi yang , zhi shi zhe li sheng ming le
  public: // operator delete
  static void * operator new(size_t size);
  static void operator delete(void *rawmemory, size_t size);
  ...
  };
  void base::operator delete(void *rawmemory, size_t size)
  {
  if (rawmemory == 0) return; // jian cha kong zhi zhen
  if (size != sizeof(base)) { // jia ru size" cuo wu ",
  ::operator delete(rawmemory); // rang biao zhun operator lai chu li qing qiu
  return;
  }
  shi fang zhi xiang rawmemory de nei cun ;
  return;
  }
  ke jian , you guan operator new he operator delete( yi ji ta men de shu zu xing shi ) de gui ding bu shi na me ma fan , chong yao de shi bi xu zun shou ta 。 zhi yao nei cun fen pei cheng xu zhi chi new-handler han shu bing zheng que di chu li le ling nei cun qing qiu , jiu cha bu duo le ; jia ru nei cun shi fang cheng xu you chu li le kong zhi zhen , na jiu mei qi ta shen me yao zuo de le 。 zhi yu zai lei cheng yuan ban ben de han shu li zeng jia ji xu zhi chi , na jiang hen kuai jiu ke yi wan cheng 。
  5. bi mian yin cang biao zhun xing shi de new
  yin wei nei bu fan wei sheng ming de ming cheng hui yin cang diao wai bu fan wei de xiang tong de ming cheng , suo yi dui yu fen bie zai lei de nei bu
  he quan ju sheng ming de liang ge xiang tong ming zi de han shu f lai shuo , lei de cheng yuan han shu hui yin cang diao quan ju han shu :
  void f(); // quan ju han shu
  class x {
  public:
  void f(); // cheng yuan han shu
  };
  
   x x;
  f(); // tiao yong f
  x.f(); // tiao yong x::f
  zhe bu hui ling ren jing qi , ye bu hui dao zhi hun xiao , yin wei tiao yong quan ju han shu he cheng yuan han shu shi zong shi cai yong bu tong de
  yu fa xing shi 。 ran er jia ru ni zai lei li zeng jia le yi ge dai duo ge shen shu de operator new han shu , jie guo jiu you
  ke neng ling ren da chi yi jing 。
  class x {
  public:
  void f();
  // operator new de shen shu zhi ding yi ge
  // new-hander(new de chu cuo chu li ) han shu
  static void * operator new(size_t size, new_handler p);
  };
  void specialerrorhandler(); // ding yi zai bie de di fang
  x *px1 =
  new (specialerrorhandler) x; // tiao yong x::operator new
  x *px2 = new x; // cuo wu !
  zai lei li ding yi le yi ge cheng wei “operator new” de han shu hou , hui bu jing yi di zu zhi le dui biao zhun new de fang
  wen 。 tiao kuan 50 jie shi le wei shen me hui zhe yang , zhe li wo men geng guan xin de shi ru he xiang ge ban fa bi mian zhe ge wen ti 。
  yi ge ban fa shi zai lei li xie yi ge zhi chi biao zhun new tiao yong fang shi de operator new, ta he biao zhun new zuo tong yang
  de shi 。 zhe ke yi yong yi ge gao xiao de nei lian han shu lai feng zhuang shi xian 。
  class x {
  public:
  void f();
  static void * operator new(size_t size, new_handler p);
  static void * operator new(size_t size)
  { return ::operator new(size); }
  };
  x *px1 =
  new (specialerrorhandler) x; // tiao yong x::operator
  // new(size_t, new_handler)
  x* px2 = new x; // tiao yong x::operator
  // new(size_t)
  ling yi zhong fang fa shi wei mei yi ge zeng jia dao operator new de shen shu ti gong que sheng zhi ( jian tiao kuan 24):
  class x {
  public:
  void f();
  static
  void * operator new(size_t size, // p que sheng zhi wei 0
  new_handler p = 0); //
  };
  x *px1 = new (specialerrorhandler) x; // zheng que
  x* px2 = new x; // ye zheng que
  wu lun na zhong fang fa , jia ru yi hou xiang dui “ biao zhun ” xing shi de new ding zhi xin de gong neng , zhi xu yao chong xie zhe ge han shu 。
  tiao yong zhe chong xin bian yi lian jie hou jiu ke yi shi yong xin gong neng le 。
  6. jia ru xie le operator new jiu yao tong shi xie operator delete
  rang wo men hui guo tou qu kan kan zhe yang yi ge ji ben wen ti : wei shen me you bi yao xie zi ji de operator new he operator delete?
  da an tong chang shi : wei le xiao lv 。 que sheng de operator new he operator delete ju you fei chang hao de tong yong xing , ta de zhe zhong ling huo xing ye shi de zai mou xie te ding de chang he xia , ke yi jin yi bu gai shan ta de xing neng 。 you qi zai na xie xu yao dong tai fen pei da liang de dan hen xiao de dui xiang de ying yong cheng xu li , qing kuang geng shi ru ci 。
  li ru you zhe yang yi ge biao shi fei ji de lei : lei airplane zhi bao han yi ge zhi zhen , ta zhi xiang de shi fei ji dui xiang de shi ji miao shu ( ci ji shu zai tiao kuan 34 jin xing shuo ming ):
  class airplanerep { ... }; // biao shi yi ge fei ji dui xiang
  //
  class airplane {
  public:
  ...
  private:
  airplanerep *rep; // zhi xiang shi ji miao shu
  };
  yi ge airplane dui xiang bing bu da , ta zhi bao han yi ge zhi zhen ( zheng ru tiao kuan 14 he m24 suo shuo ming de , jia ru airplane lei sheng ming le xu han shu , hui yin shi bao han di er ge zhi zhen )。 dan dang tiao yong operator new lai fen pei yi ge airplane dui xiang shi , de dao de nei cun ke neng yao bi cun chu zhe ge zhi zhen ( huo yi dui zhi zhen ) suo xu yao de yao duo 。 zhi suo yi hui chan sheng zhe zhong kan qi lai hen xi qi de xing wei , zai yu operator new he operator delete zhi jian xu yao hu xiang chuan di xin xi 。
  yin wei que sheng ban ben de operator new shi yi zhong tong yong xing de nei cun fen pei qi , ta bi xu ke yi fen pei ren yi da xiao de nei cun kuai 。 tong yang ,operator delete ye yao ke yi shi fang ren yi da xiao de nei cun kuai 。operator delete xiang nong qing ta yao shi fang de nei cun you duo da , jiu bi xu zhi dao dang chu operator new fen pei de nei cun you duo da 。 you yi zhong chang yong de fang fa ke yi rang operator new lai gao su operator delete dang chu fen pei de nei cun da xiao shi duo shao , jiu shi zai ta suo fan hui de nei cun li yu xian fu dai yi xie e wai xin xi , yong lai zhi ming bei fen pei de nei cun kuai de da xiao 。 ye jiu shi shuo , dang ni xie le xia mian de yu ju ,
  airplane *pa = new airplane;
  ni bu hui de dao yi kuai kan qi lai xiang zhe yang de nei cun kuai :
  pa——> airplane dui xiang de nei cun
  er shi de dao xiang zhe yang de nei cun kuai :
  pa——> nei cun kuai da xiao shu ju + airplane dui xiang de nei cun
  dui yu xiang airplane zhe yang hen xiao de dui xiang lai shuo , zhe xie e wai de shu ju xin xi hui shi de dong tai fen pei dui xiang shi suo xu yao de de nei cun de da xiao fan fan ( fei fan shi lei li mei you xu ni han shu de shi hou )。
  jia ru ruan jian yun xing zai yi ge nei cun hen bao gui de huan jing zhong , jiu cheng shou bu qi zhe zhong she chi de nei cun fen pei fang an le 。 wei airplane lei zhuan men xie yi ge operator new, jiu ke yi li yong mei ge airplane de da xiao dou xiang deng de te dian , bu bi zai mei ge fen pei de nei cun kuai shang jia shang fu dai xin xi le 。
  ju ti lai shuo , you zhe yang yi ge fang fa lai shi xian ni de zi ding yi de operator new: xian rang que sheng operator new fen pei yi xie da kuai de yuan shi nei cun , mei kuai de da xiao dou zu yi rong na hen duo ge airplane dui xiang 。airplane dui xiang de nei cun kuai jiu qu zi zhe xie da de nei cun kuai 。 dang qian mei bei shi yong de nei cun kuai bei zu zhi cheng lian biao —— cheng wei zi you lian biao —— yi bei wei lai airplane shi yong 。 ting qi lai hao xiang mei ge dui xiang dou yao cheng dan yi ge next yu de kai xiao ( yong yu zhi chi lian biao ), dan bu hui :rep yu de kong jian ye bei yong lai cun chu next zhi zhen ( yin wei zhi shi zuo wei airplane dui xiang lai shi yong de nei cun kuai cai xu yao rep zhi zhen ; tong yang , zhi you mei zuo wei airplane dui xiang shi yong de nei cun kuai cai xu yao next zhi zhen ), zhe ke yi yong union lai shi xian 。
  
  
  ju ti shi xian shi , jiu yao xiu gai airplane de ding yi , cong er zhi chi zi ding yi de nei cun zhi li 。 ke yi zhe me zuo :
  class airplane { // xiu gai hou de lei — zhi chi zi ding yi de nei cun zhi li
  public: //
  static void * operator new(size_t size);
  ...
  private:
  union {
  airplanerep *rep; // yong yu bei shi yong de dui xiang
  airplane *next; // yong yu mei bei shi yong de ( zai zi you lian biao zhong ) dui xiang
  };
  // lei de chang liang , zhi ding yi ge da de nei cun kuai zhong fang duo shao ge
  // airplane dui xiang , zai hou mian chu shi hua
  static const int block_size;
  static airplane *headoffreelist;
  };
  shang mian de dai ma zeng jia le de ji ge sheng ming : yi ge operator new han shu , yi ge lian he ( shi de rep he next yu zhan yong tong yang de kong jian ), yi ge chang liang ( zhi ding da nei cun kuai de da xiao ), yi ge jing tai zhi zhen ( gen zong zi you lian biao de biao tou )。 biao tou zhi zhen sheng ming wei jing tai cheng yuan hen chong yao , yin wei zheng ge lei zhi you yi ge zi you lian biao , er bu shi mei ge airplane dui xiang dou you 。
  xia mian gai xie operator new han shu le :
  void * airplane::operator new(size_t size)
  {
  // ba “ cuo wu ” da xiao de qing qiu zhuan gei ::operator new() chu li ;
  // xiang jian tiao kuan 8
  if (size != sizeof(airplane))
  return ::operator new(size);
  airplane *p = // p zhi xiang zi you lian biao de biao tou
  headoffreelist; //
  // p ruo he fa , ze jiang biao tou yi dong dao ta de xia yi ge yuan su
  //
  if (p)
  headoffreelist = p->next;
  else {
  // zi you lian biao wei kong , ze fen pei yi ge da de nei cun kuai ,
  // ke yi rong na block_size ge airplane dui xiang
  airplane *newblock =
  static_cast(::operator new(block_size *
  sizeof(airplane)));
  // jiang mei ge xiao nei cun kuai lian jie qi lai xing cheng yi ge xin de zi you lian biao
  // tiao guo di 0 ge yuan su , yin wei ta yao bei fan hui gei operator new de tiao yong zhe
  //
  for (int i = 1; i < block_size-1; ++i)
  newblock[i].next = &newblock[i+1];
  // yong kong zhi zhen jie shu lian biao
  newblock[block_size-1].next = 0;
  // p she wei biao de tou bu ,headoffreelist zhi xiang de
  // nei cun kuai jin gen qi hou
  p = newblock;
  headoffreelist = &newblock[1];
  }
  return p;
  }
  jia ru ni du le tiao kuan 8, jiu hui zhi dao zai operator new bu neng man zu nei cun fen pei qing qiu shi , hui zhi xing yi xi lie yu new-handler han shu he li wai you guan de li xing xing dong zuo 。 shang mian de dai ma mei you zhe xie bu zhou , zhe shi yin wei operator new zhi li de nei cun dou shi cong ::operator new fen pei lai de 。 zhe yi wei zhe zhi you ::operator new shi bai shi ,operator new cai hui shi bai 。 er jia ru ::operator new shi bai , ta hui qu zhi xing new-handler de dong zuo ( ke neng zui hou yi pao chu yi chang jie shu ), suo yi bu xu yao airplane de operator new ye qu chu li 。 huan ju hua shuo , qi shi new-handler de dong zuo dou huan zai , ni zhi shi mei kan jian , ta yin cang zai ::operator new li 。
  you le operator new, xia mian yao zuo de jiu shi gei chu airplane de jing tai shu ju cheng yuan de ding yi :
  airplane *airplane::headoffreelist;
  const int airplane::block_size = 512;
  mei bi yao xian shi di jiang headoffreelist she zhi wei kong zhi zhen , yin wei jing tai cheng yuan de chu shi zhi dou bei que sheng she wei 0。block_size jue ding le yao cong ::operator new huo de duo da de nei cun kuai 。
  zhe ge ban ben de operator new jiang hui gong zuo de fei chang hao 。 ta wei airplane dui xiang fen pei de nei cun yao bi que sheng operator new geng shao , er qie yun xing de geng kuai , ke neng hui kuai 2 ci fang de deng ji 。 zhe mei shen me xi qi de , tong yong xing de que sheng operator new bi xu ying fu ge zhong da xiao de nei cun qing qiu , huan yao chu li nei bu wai bu de sui pian ; er ni de operator new zhi yong cao zuo lian biao zhong de yi dui zhi zhen 。 pao qi ling huo xing wang wang ke yi hen qing yi di huan lai su du 。
  xia mian wo men jiang tao lun operator delete。 huan ji de operator delete ma ? ben tiao kuan jiu shi guan yu operator delete de tao lun 。 dan zhi dao xian zai wei zhi ,airplane lei zhi sheng ming le operator new, huan mei sheng ming operator delete。 xiang xiang jia ru xie le xia mian de dai ma hui fa sheng shen me :
  airplane *pa = new airplane; // tiao yong
  // airplane::operator new
  ...
  delete pa; // tiao yong ::operator delete
  du zhe duan dai ma shi , jia ru ni shu qi er duo , hui ting dao fei ji zhuang hui ran shao de sheng yin , huan you cheng xu yuan de ku qi 。 wen ti chu zai operator new( zai airplane li ding yi de na ge ) fan hui le yi ge bu dai tou xin xi de nei cun de zhi zhen , er operator delete( que sheng de na ge ) que jia she chuan gei ta de nei cun bao han tou xin xi 。 zhe jiu shi bei ju chan sheng de yuan yin 。
  zhe ge li zi shuo ming le yi ge pu bian yuan ze :operator new he operator delete bi xu tong shi xie , zhe yang cai bu hui chu xian bu tong de jia she 。 jia ru xie le yi ge zi ji de nei cun fen pei cheng xu , jiu yao tong shi xie yi ge shi fang cheng xu 。( guan yu wei shen me yao zun xun zhe tiao gui ding de ling yi ge li you , shen jian article on counting objects yi wen de the sidebar on placement zhang jie )
  yin er , ji xu she ji airplane lei ru xia :
  class airplane { // he qian mian de yi yang , zhi bu guo zeng jia le yi ge
  public: // operator delete de sheng ming
  ...
  static void operator delete(void *deadobject,
  size_t size);
  };
  // chuan gei operator delete de shi yi ge nei cun kuai , jia ru
  // qi da xiao zheng que , jiu jia dao zi you nei cun kuai lian biao de zui qian mian
  //
  void airplane::operator delete(void *deadobject,
  
   size_t size)
  {
  if (deadobject == 0) return; // jian tiao kuan 8
  if (size != sizeof(airplane)) { // jian tiao kuan 8
  ::operator delete(deadobject);
  return;
  }
  airplane *carcass =
  static_cast(deadobject);
  carcass->next = headoffreelist;
  headoffreelist = carcass;
  }
  yin wei qian mian zai operator new li jiang “ cuo wu ” da xiao de qing qiu zhuan gei le quan ju operator new( jian tiao kuan 8), na me zhe li tong yang yao jiang “ cuo wu ” da xiao de dui xiang jiao gei quan ju operator delete lai chu li 。 jia ru bu zhe yang , jiu hui chong xian ni qian mian fei jin xin si xiang bi mian de na zhong wen ti ——new he delete ju fa shang de bu pi pei 。
  you qu de shi , jia ru yao shan chu de dui xiang shi cong yi ge mei you xu xi gou han shu de lei ji xu er lai de , na chuan gei operator delete de size_t zhi you ke neng bu zheng que 。 zhe jiu shi bi xu bao zheng ji lei bi xu yao you xu xi gou han shu de yuan yin , ci wai tiao kuan 14 huan lie chu le di er ge 、 li you geng chong zu de yuan yin 。 zhe li zhi yao jian dan di ji zhu , ji lei jia ru yi lou le xu ni gou han shu ,operator delete jiu you ke neng gong zuo bu zheng que 。
  suo you yi qie dou hen hao , dan cong ni zhou qi de mei tou wo ke yi zhi dao ni yi ding zai dan xin nei cun xie lu 。 you zhe da liang kai fa jing yan de ni bu hui mei zhu chong dao ,airplane de operator new tiao yong ::operator new de dao le da kuai nei cun , dan airplane de operator delete que mei you shi fang ta men 。 nei cun xie lu ! nei cun xie lu ! wo fen ming ting jian le jing zhong zai ni nao hai li hui xiang 。
  dan qing zai xi ting wo hui da , zhe li mei you nei cun xie lu !
  yin qi nei cun xie lu de yuan yin zai yu nei cun fen pei hou zhi xiang nei cun de zhi zhen diu shi le 。 jia ru mei you la ji chu li huo qi ta yu yan zhi wai de ji zhi , zhe xie nei cun jiu bu hui bei shou hui 。 dan shang mian de she ji mei you nei cun xie lu , yin wei ta jue bu hui chu xian nei cun zhi zhen diu shi de qing kuang 。 mei ge da nei cun kuai shou xian bei fen cheng airplane da xiao de xiao kuai , ran hou zhe xie xiao kuai bei fang zai zi you lian biao shang 。 dang ke hu tiao yong airplane::operator new shi , xiao kuai bei zi you lian biao yi chu , ke hu de dao zhi xiang xiao kuai de zhi zhen 。 dang ke hu tiao yong operator delete shi , xiao kuai bei fang hui dao zi you lian biao shang 。 cai yong zhe zhong she ji , suo you de nei cun kuai yao bu bei airplane dui xiang shi yong ( zhe zhong qing kuang xia , shi you ke hu lai fu ze bi mian nei cun xie lu ), yao bu jiu zai zi you lian biao shang ( zhe zhong qing kuang xia nei cun kuai you zhi zhen )。 suo yi shuo zhe li mei you nei cun xie lu 。
  ran er que shi ,::operator new fan hui de nei cun kuai shi cong lai mei you bei airplane::operator delete shi fang , zhe ge nei cun kuai you ge ming zi , jiao nei cun chi 。 dan nei cun xie lou he nei cun chi you yi ge chong yao de bu tong zhi chu 。 nei cun xie lou hui wu xian di zeng chang , ji shi ke hu xun gui dao ju ; er nei cun chi de da xiao jue bu hui chao guo ke hu qing qiu nei cun de zui da zhi 。
  xiu gai airplane de nei cun zhi li cheng xu shi de ::operator new fan hui de nei cun kuai zai bu bei shi yong shi zi dong shi fang bing bu nan , dan zhe li bu hui zhe me zuo , zhe you liang ge yuan yin : di yi ge yuan yin he ni zi ding yi nei cun zhi li de chu zhong you guan 。 ni you hen duo li you qu zi ding yi nei cun zhi li , zui ji ben de yi tiao shi ni que ren que sheng de operator new he operator delete shi yong le tai duo de nei cun huo ( bing qie ) yun xing hen man 。 he cai yong nei cun chi ce lue xiang bi , gen zong he shi fang na xie da nei cun kuai suo xie de mei yi ge e wai de zi jie he mei yi tiao e wai de yu ju dou hui dao zhi ruan jian yun xing geng man , yong de nei cun geng duo 。 zai she ji xing neng yao qiu hen gao de ku huo cheng xu shi , jia ru ni yu ji nei cun chi de da xiao hui zai yi ge he li de fan wei zhi nei , na cai yong nei cun chi de fang fa zai hao bu guo le 。
  di er ge yuan yin he chu li yi xie bu he li de cheng xu xing wei you guan 。 jia she airplane de nei cun zhi li cheng xu bei xiu gai le ,airplane de operator delete ke yi shi fang ren he mei you dui xiang cun zai de da kuai de nei cun 。 na kan xia mian de cheng xu : int main()
  {
  airplane *pa = new airplane; // di yi ci fen pei : de dao da kuai nei cun ,
  // sheng cheng zi you lian biao , deng
  delete pa; // nei cun kuai kong ;
  // shi fang ta
  pa = new airplane; // zai ci de dao da kuai nei cun ,
  // sheng cheng zi you lian biao , deng
  delete pa; // nei cun kuai zai ci kong ,
  // shi fang
  ... // ni you le xiang fa ...
  return 0;
  }
  zhe ge zao gao de xiao cheng xu hui bi yong que sheng de operator new he operator delete xie de cheng xu yun xing de huan man , zhan yong huan yao duo de nei cun , geng bu yao he yong nei cun chi xie de cheng xu bi le 。
  dang ran you ban fa chu li zhe zhong bu he li de qing kuang , dan kao lv de fei fan qing kuang yue duo , jiu yue you ke neng yao chong xin shi xian nei cun zhi li han shu , er zui hou ni you hui de dao shen me ne ? nei cun chi bu neng jie jue suo you de nei cun zhi li wen ti , zai hen duo qing kuang xia shi hen shi he de 。
  shi ji kai fa zhong , ni hui jing chang yao gei xu duo bu tong de lei shi xian ji yu nei cun chi de gong neng 。 ni hui xiang ,“ yi ding you shen me ban fa ba zhe zhong gu ding da xiao nei cun de fen pei qi feng zhuang qi lai , cong er ke yi fang bian di shi yong ”。 shi de , you ban fa 。 sui ran wo zai zhe ge tiao kuan yi jing lao tao zhe me chang shi jian le , dan huan shi yao jian dan jie shao yi xia , ju ti shi xian liu gei du zhe zuo lian xi 。
  xia mian jian dan gei chu le yi ge pool lei de zui xiao jie kou ( jian tiao kuan 18),pool lei de mei ge dui xiang shi mou lei dui xiang ( qi da xiao zai pool de gou zao han shu li zhi ding ) de nei cun fen pei qi 。
  class pool {
  public:
  pool(size_t n); // wei da xiao wei n de dui xiang chuang jian
  // yi ge fen pei qi
  void * alloc(size_t n) ; // wei yi ge dui xiang fen pei zu gou nei cun
  // zun xun tiao kuan 8 de operator new chang gui
  void free( void *p, size_t n); // jiang p suo zhi de nei cun fan hui dao nei cun chi ;
  // zun xun tiao kuan 8 de operator delete chang gui
  ~pool(); // shi fang nei cun chi zhong quan bu nei cun
  };
  zhe ge lei zhi chi pool dui xiang de chuang jian , zhi xing fen pei he shi fang cao zuo , yi ji bei cui hui 。pool dui xiang bei cui hui shi , hui shi fang ta fen pei de suo you nei cun 。 zhe jiu shi shuo , xian zai you ban fa bi mian airplane de han shu li suo biao xian de nei cun xie lou si de xing wei le 。 ran er zhe ye yi wei zhe , jia ru pool de xi gou han shu tiao yong tai kuai ( shi yong nei cun chi de dui xiang mei you quan bu bei cui hui ), yi xie dui xiang jiu hui fa xian ta zheng zai shi yong de nei cun meng ran jian mei le 。 zhe zao cheng de jie guo tong chang shi bu ke cai ce de 。
  you le zhe ge pool lei , ji shi Java cheng xu yuan ye ke yi bu fei chui hui zhi li di zai airplane lei li zeng jia zi ji de nei cun zhi li gong neng :
  class airplane {
  public:
  ... // pu tong airplane gong neng
  static void * operator new(size_t size);
  static void operator delete(void *p, size_t size);
  
  
  private:
  airplanerep *rep; // zhi xiang shi ji miao shu de zhi zhen
  static pool mempool; // airplanes de nei cun chi
  };
  inline void * airplane::operator new(size_t size)
  { return mempool.alloc(size); }
  inline void airplane::operator delete(void *p,
  size_t size)
  { mempool.free(p, size); }
  // wei airplane dui xiang chuang jian yi ge nei cun chi ,
  // zai lei de shi xian wen jian li shi xian
  pool airplane::mempool(sizeof(airplane));
  zhe ge she ji bi qian mian de yao qing chu 、 gan jing de duo , yin wei airplane lei bu zai he fei airplane de dai ma hun zai yi qi 。union, zi you lian biao tou zhi zhen , ding yi yuan shi nei cun kuai da xiao de chang liang dou bu jian le , ta men dou yin cang zai ta men ying gai dai de di fang ——pool lei li 。 rang xie pool de cheng xu yuan qu cao xin nei cun zhi li de xi jie ba , ni de gong zuo zhi shi rang airplane lei zheng chang gong zuo 。
  xian zai ying gai ming bai le , zi ding yi de nei cun zhi li cheng xu ke yi hen hao di gai shan cheng xu de xing neng , er qie ta men ke yi feng zhuang zai xiang pool zhe yang de lei li 。 dan qing bu yao wang ji zhu yao de yi dian ,operator new he operator delete xu yao tong shi gong zuo , na me ni xie le operator new, jiu ye yi ding yao xie operator delete。
  ban quan wen ti :Effective c++ second edition zhong wen ban【原文】【汉音对照
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
1. dui ying de new he delete yao cai yong xiang tong de xing shi     xia mian de yu ju you shen me cuo ? string *stringarray = new string[100]; ... delete stringarray; yi qie hao xiang dou jing ran you xu —— yi ge new dui ying zhe yi ge delete—— ran er que yin cang zhe hen da de cuo wu : cheng xu de yun xing qing kuang jiang shi bu ke cai ce de 。 zhi shao ,stringarray zhi xiang de 100 ge string dui xiang zhong de 99 ge bu hui bei zheng que di cui hui , yin wei ta men de xi gou han shu yong yuan bu hui bei tiao yong 。 yong new de shi hou hui fa sheng liang jian shi 。 shou xian , nei cun bei fen pei ( tong guo operator new han shu , xiang jian tiao kuan 7-10 he tiao kuan m8), ran hou , wei bei fen pei de nei cun tiao yong yi ge huo duo ge gou zao han shu 。 yong delete de shi hou , ye you liang jian shi fa sheng : shou xian , wei jiang bei shi fang de nei cun tiao yong yi ge huo duo ge xi gou han shu , ran hou , shi fang nei cun ( tong guo operator delete han shu , xiang jian tiao kuan 8 he m8)。 dui yu delete lai shuo hui you zhe yang yi ge chong yao de wen ti : nei cun zhong you duo shao ge dui xiang yao bei shan chu ? da an jue ding le jiang you duo shao ge xi gou han shu hui bei tiao yong 。 zhe ge wen ti jian dan lai shuo jiu shi : yao bei shan chu de zhi zhen zhi xiang de shi dan ge dui xiang ne , huan shi dui xiang shu zu ? zhe zhi you ni lai gao su delete。 jia ru ni zai yong delete shi mei yong kuo hao ,delete jiu hui ren wei zhi xiang de shi dan ge dui xiang , fou ze , ta jiu hui ren wei zhi xiang de shi yi ge shu zu : string *stringptr1 = new string; string *stringptr2 = new string[100]; ... delete stringptr1;// shan chu yi ge dui xiang delete [] stringptr2;// shan chu dui xiang shu zu jia ru ni zai stringptr1 qian jia le "[]" hui zen yang ne ? da an shi : na jiang shi bu ke cai ce de ; jia ru ni mei zai stringptr2 qian mei jia shang "[]" you hui zen yang ne ? da an ye shi : bu ke cai ce 。 er qie dui yu xiang int zhe yang de gu ding lei xing lai shuo , jie guo ye shi bu ke cai ce de , ji shi zhe yang de lei xing mei you xi gou han shu 。 suo yi , jie jue zhe lei wen ti de gui ze hen jian dan : jia ru ni tiao yong new shi yong le [], tiao yong delete shi ye yao yong []。 jia ru tiao yong new shi mei you yong [], na tiao yong delete shi ye bu yao yong []。 zai xie yi ge bao han zhi zhen shu ju cheng yuan , bing qie ti gong duo ge gou zao han shu de lei shi , lao ji zhe yi gui ze you qi chong yao 。 yin wei zhe yang de hua , ni jiu bi xu zai suo you chu shi hua zhi zhen cheng yuan de gou zao han shu li cai yong xiang tong de new de xing shi 。 fou ze , xi gou han shu li jiang cai yong shen me xing shi de delete ne ? guan yu zhe yi hua ti de jin yi bu chan shu , shen jian tiao kuan 11。 zhe ge gui ze dui xi huan yong typedef de ren lai shuo ye hen chong yao , yin wei xie typedef de cheng xu yuan bi xu gao su bie ren , yong new chuang jian le yi ge typedef ding yi de lei xing de dui xiang hou , gai yong shen me xing shi de delete lai shan chu 。 ju li ru xia : typedef string addresslines[4]; // yi ge ren de di zhi , gong 4 xing , mei xing yi ge string // yin wei addresslines shi ge shu zu , shi yong new: string *pal = new addresslines; // zhu chong "new addresslines" fan hui string*, he // "new string[4]" fan hui de yi yang delete shi bi xu yi shu zu xing shi yu zhi dui ying : delete pal;// cuo wu ! delete [] pal;// zheng que wei le bi mian hun luan , zui hao du jue dui shu zu lei xing yong typedefs。 zhe qi shi hen qing yi , yin wei biao zhun c++ ku ( jian tiao kuan 49) bao han you stirng he vector mo ban , shi yong ta men jiang hui shi dui shu zu de xu qiu jian shao dao ji hu ling 。 ju li lai shuo ,addresslines ke yi ding yi wei yi ge zi fu chuan (string) de xiang liang (vector), ji addresslines ke ding yi wei vector lei xing 。 2. xi gou han shu li dui zhi zhen cheng yuan tiao yong delete da duo shu qing kuang xia , zhi xing dong tai nei cun fen pei de de lei dou zai gou zao han shu li yong new fen pei nei cun , ran hou zai xi gou han shu li yong delete shi fang nei cun 。 zui chu xie zhe ge lei de shi hou dang ran bu nan zuo , ni hui ji de zui hou dui zai suo you gou zao han shu li fen pei le nei cun de suo you cheng yuan shi yong delete。 ran er , zhe ge lei jing guo wei hu 、 sheng ji hou , qing kuang jiu hui bian de kun nan le , yin wei dui lei de dai ma jin xing xiu gai de cheng xu yuan bu yi ding jiu shi zui zao xie zhe ge lei de ren 。 er zeng jia yi ge zhi zhen cheng yuan yi wei zhe ji hu dou yao jin xing xia mian de gong zuo : · zai mei ge gou zao han shu li dui zhi zhen jin xing chu shi hua 。 dui yu yi xie gou zao han shu , jia ru mei you nei cun yao fen pei gei zhi zhen de hua , zhi zhen yao bei chu shi hua wei 0( ji kong zhi zhen )。 · shan chu xian you de nei cun , tong guo fu zhi cao zuo fu fen pei gei zhi zhen xin de nei cun 。 · zai xi gou han shu li shan chu zhi zhen 。 jia ru zai gou zao han shu li wang le chu shi hua mou ge zhi zhen , huo zhe zai fu zhi cao zuo de guo cheng zhong wang le chu li ta , wen ti hui chu xian de hen kuai , hen ming xian , suo yi zai shi jian zhong zhe liang ge wen ti bu hui na me zhe mo ni 。 dan shi , jia ru zai xi gou han shu li mei you shan chu zhi zhen , ta bu hui biao xian chu hen ming xian de wai bu zheng zhuang 。 xiang fan , ta ke neng zhi shi biao xian wei yi dian wei xiao de nei cun xie lu , bing qie bu duan zeng chang , zui hou tun shi le ni de di zhi kong jian , dao zhi cheng xu yao zhe 。 yin wei zhe zhong qing kuang jing chang bu na me yin ren zhu chong , suo yi mei zeng jia yi ge zhi zhen cheng yuan dao lei li shi yi ding yao ji qing chu 。 ling wai , shan chu kong zhi zhen shi an quan de ( yin wei ta shen me ye mei zuo )。 suo yi , zai xie gou zao han shu , fu zhi cao zuo fu , huo qi ta cheng yuan han shu shi , lei de mei ge zhi zhen cheng yuan yao me zhi xiang you xiao de nei cun , yao me jiu zhi xiang kong , na zai ni de xi gou han shu li ni jiu ke yi zhi yong jian dan di delete diao ta men , er bu yong dan xin ta men shi bu shi bei new guo 。 dang ran dui ben tiao kuan de shi yong ye bu yao jue dui 。 li ru , ni dang ran bu hui yong delete qu shan chu yi ge mei you yong new lai chu shi hua de zhi zhen , er qie , jiu xiang yong zhi neng zhi zhen dui xiang shi bu yong lao ni qu shan chu yi yang , ni ye yong yuan bu hui qu shan chu yi ge chuan di gei ni de zhi zhen 。 huan ju hua shuo , chu fei lei cheng yuan zui chu yong le new, fou ze shi bu yong zai xi gou han shu li yong delete de 。 shuo dao zhi neng zhi zhen , zhe li jie shao yi zhong bi mian bi xu shan chu zhi zhen cheng yuan de fang fa , ji ba zhe xie cheng yuan yong zhi neng zhi zhen dui xiang lai dai ti , bi ru c++ biao zhun ku li de auto_ptr。 xiang zhi dao ta shi ru he gong zuo de , kan kan tiao kuan m9 he m10。 3. yu xian yu bei hao nei cun bu gou de qing kuang             operator new zai wu fa wan cheng nei cun fen pei qing qiu shi hui pao chu yi chang ( yi qian de zuo fa yi ban shi fan hui 0, yi xie jiu yi dian de bian yi qi huan zhe me zuo 。 ni yuan yi de hua ye ke yi ba ni de bian yi qi she zhi cheng zhe yang 。 guan yu zhe ge hua ti wo jiang tui chi dao ben tiao kuan de jie yi chu tao lun )。 da jia dou zhi dao , chu li nei cun bu gou suo chan sheng de yi chang zhen ke yi suan de shang shi ge dao de shang de xing wei , dan shi ji zuo qi lai you hui xiang dao jia zai bo zi shang na yang tong ku 。 suo yi , ni you shi hui bu qu guan ta , ye xu yi zhi mei qu guan ta 。 dan ni xin li yi ding huan shi shen shen di yin cang zhe yi zhong zui e gan : wan yi new zhen de chan sheng le yi chang zen me ban ? ni hui hen zi ran di xiang dao chu li zhe zhong qing kuang de yi zhong fang fa , ji hui dao yi qian de lao lu shang qu , shi yong yu chu li 。 li ru ,c de yi zhong chang yong de zuo fa shi , ding yi yi ge lei xing wu guan de hong lai fen pei nei cun bing jian cha fen pei shi fou cheng gong 。 dui yu c++ lai shuo , zhe ge hong kan qi lai ke neng xiang zhe yang : #define new(ptr, type) try { (ptr) = new type; } catch (std::bad_alloc&) { assert(0); } (“ man !std::bad_alloc shi zuo shen me de ?” ni hui wen 。bad_alloc shi operator new bu neng man zu nei cun fen pei qing qiu shi pao chu de yi chang lei xing ,std shi bad_alloc suo zai de ming zi kong jian ( jian tiao kuan 28) de ming cheng 。“ hao !” ni hui ji xu wen ,“assert you you shen me yong ?” jia ru ni kan kan biao zhun c tou wen jian ( huo yu ta xiang deng jia de yong dao le ming zi kong jian de ban ben , jian tiao kuan 49), jiu hui fa xian assert shi ge hong 。 zhe ge hong jian cha chuan gei ta de biao da shi shi fou fei ling , jia ru bu shi fei ling zhi , jiu hui fa chu yi tiao chu cuo xin xi bing tiao yong abort。assert zhi shi zai mei ding yi biao zhun hong ndebug de shi hou , ji zai tiao shi zhuang tai xia cai zhe me zuo 。 zai chan pin fa bu zhuang tai xia , ji ding yi le ndebug de shi hou ,assert shen me ye bu zuo , xiang dang yu yi tiao kong yu ju 。 suo yi ni zhi neng zai tiao shi shi cai neng jian cha duan yan (assertion))。 new hong bu dan you zhe shang mian suo shuo de tong bing , ji yong assert qu jian cha ke neng fa sheng zai yi fa bu cheng xu li de zhuang tai ( ran er ren he shi hou dou ke neng fa sheng nei cun bu gou de qing kuang ), tong shi , ta huan zai c ++ li you ling wai yi ge que xian : ta mei you kao lv dao new you ge zhong ge yang de shi yong fang shi 。 li ru , xiang chuang jian lei xing t dui xiang , yi ban you san zhong chang jian de yu fa xing shi , ni bi xu dui mei zhong xing shi ke neng chan sheng de yi chang dou yao jin xing chu li : new t; new t(constrUCtor arguments); new t[size]; zhe li dui wen ti da da jin xing le jian hua , yin wei you ren huan hui zi ding yi ( chong zai )operator new, suo yi cheng xu li hui bao han ren yi ge shi yong new de yu fa xing shi 。 na me , zen me ban ? jia ru xiang yong yi ge hen jian dan de chu cuo chu li fang fa , ke yi zhe me zuo : dang nei cun fen pei qing qiu bu neng man zu shi , tiao yong ni yu xian zhi ding de yi ge chu cuo chu li han shu 。 zhe ge fang fa ji yu yi ge chang gui , ji dang operator new bu neng man zu qing qiu shi , hui zai pao chu yi chang zhi qian tiao yong ke hu zhi ding de yi ge chu cuo chu li han shu —— yi ban cheng wei new-handler han shu 。(operator new shi ji gong zuo qi lai yao fu duo yi xie , xiang jian tiao kuan 8) zhi ding chu cuo chu li han shu shi yao yong dao set_new_handler han shu , ta zai tou wen jian li da zhi shi xiang xia mian zhe yang ding yi de : typedef void (*new_handler)(); new_handler set_new_handler(new_handler p) throw(); ke yi kan dao ,new_handler shi yi ge zi ding yi de han shu zhi zhen lei xing , ta zhi xiang yi ge mei you shu ru shen shu ye mei you fan hui zhi de han shu 。set_new_handler ze shi yi ge shu ru bing fan hui new_handler lei xing de han shu 。 set_new_handler de shu ru shen shu shi operator new fen pei nei cun shi bai shi yao tiao yong de chu cuo chu li han shu de zhi zhen , fan hui zhi shi set_new_handler mei tiao yong zhi qian jiu yi jing zai qi zuo yong de jiu de chu cuo chu li han shu de zhi zhen 。 ke yi xiang xia mian zhe yang shi yong set_new_handler: // function to call if operator new can't allocate enough memory void nomorememory() { cerr << "unable to satisfy request for memory "; abort(); } int main() { set_new_handler(nomorememory); int *pbigdataarray = new int[100000000]; ... } jia ru operator new bu neng wei 100,000,000 ge zheng shu fen pei kong jian ,nomorememory jiang hui bei tiao yong , cheng xu fa chu yi tiao chu cuo xin xi hou zhong zhi 。 zhe jiu bi jian dan di rang xi tong nei gai chan sheng cuo wu xin xi lai jie shu cheng xu yao hao 。( shun bian kao lv yi xia , jia ru cerr zai xie cuo wu xin xi de guo cheng zhong yao dong tai fen pei nei cun , na jiang hui fa sheng shen me ...) operator new bu neng man zu nei cun fen pei qing qiu shi ,new-handler han shu bu zhi tiao yong yi ci , er shi bu duan chong fu , zhi zhi zhao dao zu gou de nei cun 。 shi xian chong fu tiao yong de dai ma zai tiao kuan 8 li ke yi kan dao , zhe li wo yong miao shu xing de de yu yan lai shuo ming : yi ge she ji de hao de new-handler han shu bi xu shi xian xia mian gong neng zhong de yi zhong 。 · chan sheng geng duo de ke yong nei cun 。 zhe jiang shi operator new xia yi ci fen pei nei cun de chang shi you ke neng huo de cheng gong 。 shi shi zhe yi ce lue de yi ge fang fa shi : zai cheng xu qi dong shi fen pei yi ge da de nei cun kuai , ran hou zai di yi ci tiao yong new-handler shi shi fang 。 shi fang shi ban sui zhe yi xie dui yong hu de jing gao xin xi , ru nei cun shu liang tai shao , xia ci qing qiu ke neng hui shi bai , chu fei you you geng duo de ke yong kong jian 。 · an zhuang ling yi ge bu tong de new-handler han shu 。 jia ru dang qian de new-handler han shu bu neng chan sheng geng duo de ke yong nei cun , ke neng ta hui zhi dao ling yi ge new- handler han shu ke yi ti gong geng duo de zi yuan 。 zhe yang de hua , dang qian de new-handler ke yi an zhuang ling yi ge new-handler lai qu dai ta ( tong guo tiao yong set_new_handler)。 xia yi ci operator new tiao yong new-handler shi , hui shi yong zui jin an zhuang de na ge 。( zhe yi ce lue de ling yi ge bian tong ban fa shi rang new-handler ke yi gai bian ta zi ji de yun xing xing wei , na me xia ci tiao yong shi , ta jiang zuo bu tong de shi 。 fang fa shi shi new-handler ke yi xiu gai na xie ying xiang ta zi shen xing wei de jing tai huo quan ju shu ju 。) · xie chu new-handler。 ye jiu shi chuan di kong zhi zhen gei set_new_handler。 mei you an zhuang new-handler,operator new fen pei nei cun bu cheng gong shi jiu hui pao chu yi ge biao zhun de std::bad_alloc lei xing de yi chang 。 · pao chu std::bad_alloc huo cong std::bad_alloc ji xu de qi ta lei xing de yi chang 。 zhe yang de yi chang bu hui bei operator new bu zhuo , suo yi ta men hui bei song dao zui chu jin xing nei cun qing qiu de di fang 。( pao chu bie de bu tong lei xing de yi chang hui wei fan operator new yi chang gui fan 。 gui fan zhong de que sheng xing wei shi tiao yong abort, suo yi new-handler yao pao chu yi ge yi chang shi , yi ding yao que xin ta shi cong std::bad_alloc ji xu lai de 。 xiang geng duo di le jie yi chang gui fan , shen jian tiao kuan m14。) · mei you fan hui 。 dian xing zuo fa shi tiao yong abort huo exit。abort/exit ke yi zai biao zhun c ku zhong zhao dao ( huan you biao zhun c++ ku , shen jian tiao kuan 49)。 shang mian de xuan ze gei le ni shi xian new-handler han shu ji da de ling huo xing 。 chu li nei cun fen pei shi bai de qing kuang shi cai qu shen me fang fa , qu jue yu yao fen pei de dui xiang de lei : class x { public: static void outofmemory(); ... }; class y { public: static void outofmemory(); ... }; x* p1 = new x; // ruo fen pei cheng gong , tiao yong x::outofmemory y* p2 = new y; // ruo fen pei bu cheng gong , tiao yong y::outofmemory c++ bu zhi chi zhuan men zhen dui yu lei de new-handler han shu , er qie ye bu xu yao 。 ni ke yi zi ji lai shi xian ta , zhi yao zai mei ge lei zhong ti gong zi ji ban ben de set_new_handler he operator new。 lei de set_new_handler ke yi wei lei zhi ding new-handler( jiu xiang biao zhun de set_new_handler zhi ding quan ju new-handler yi yang )。 lei de operator new ze bao zheng wei lei de dui xiang fen pei nei cun shi yong lei de new-handler qu dai quan ju new-handler。 jia she chu li lei x nei cun fen pei shi bai de qing kuang 。 yin wei operator new dui lei xing x de dui xiang fen pei nei cun shi bai shi , mei ci dou bi xu tiao yong chu cuo chu li han shu , suo yi yao zai lei li sheng ming yi ge new_handler lei xing de jing tai cheng yuan 。 na me lei x kan qi lai hui xiang zhe yang : class x { public: static new_handler set_new_handler(new_handler p); static void * operator new(size_t size); private: static new_handler currenthandler; }; lei de jing tai cheng yuan bi xu zai lei wai ding yi 。 yin wei xiang jie yong jing tai dui xiang de que sheng chu shi hua zhi 0, suo yi ding yi x::currenthandler shi mei you qu chu shi hua 。 new_handler x::currenthandler; // que sheng she zhi currenthandler wei 0( ji null) lei x zhong de set_new_handler han shu hui bao cun chuan gei ta de ren he zhi zhen , bing fan hui zai tiao yong ta zhi qian suo bao cun de ren he zhi zhen 。 zhe zheng shi biao zhun ban ben de set_new_handler suo zuo de : new_handler x::set_new_handler(new_handler p) { new_handler oldhandler = currenthandler; currenthandler = p; return oldhandler; } zui hou kan kan x de operator new suo zuo de : 1. tiao yong biao zhun set_new_handler han shu , shu ru shen shu wei x de chu cuo chu li han shu 。 zhe shi de x de new-handler han shu cheng wei quan ju new-handler han shu 。 zhu chong xia mian de dai ma zhong , yong le "::" fu hao xian shi di yin yong std kong jian ( biao zhun set_new_handler han shu jiu cun zai yu std kong jian )。 2. tiao yong quan ju operator new fen pei nei cun 。 jia ru di yi ci fen pei shi bai , quan ju operator new hui tiao yong x de new-handler, yin wei ta gang gang ( jian 1.) bei an zhuang cheng wei quan ju new-handler。 jia ru quan ju operator new zui zhong wei neng fen pei dao nei cun , ta pao chu std::bad_alloc yi chang ,x de operator new hui bu zhuo dao ta 。x de operator new ran hou hui fu zui chu bei qu dai de quan ju new-handler han shu , zui hou yi pao chu yi chang fan hui 。 3. jia she quan ju operator new wei lei xing x de dui xiang fen pei nei cun cheng gong ,, x de operator new hui zai ci tiao yong biao zhun set_new_handler lai hui fu zui chu de quan ju chu cuo chu li han shu 。 zui hou fan hui fen pei cheng gong de nei cun de zhi zhen 。 c++ shi zhe me zuo de : void * x::operator new(size_t size) { new_handler globalhandler = // an zhuang x de new_handler std::set_new_handler(currenthandler); void *memory; try { // chang shi fen pei nei cun memory = ::operator new(size); } catch (std::bad_alloc&) { // hui fu jiu de new_handler std::set_new_handler(globalhandler); throw; // pao chu yi chang } std::set_new_handler(globalhandler); // hui fu jiu de new_handler return memory; } jia ru ni dui shang mian chong fu tiao yong std::set_new_handler kan bu shun yan , ke yi shen jian tiao kuan m9 lai chu qu ta men 。 shi yong lei x de nei cun fen pei chu li gong neng shi da zhi ru xia : void nomorememory();// x de dui xiang fen pei nei cun shi bai shi tiao yong de new_handler han shu de sheng ming x::set_new_handler(nomorememory); // ba nomorememory she zhi wei x de // new-handling han shu x *px1 = new x; // ru nei cun fen pei shi bai , // tiao yong nomorememory string *ps = new string; // ru nei cun fen pei shi bai , tiao yong quan ju new-handling han shu x::set_new_handler(0); // she x de new-handling han shu wei kong x *px2 = new x; // ru nei cun fen pei shi bai , li ji pao chu yi chang // ( lei x mei you new-handling han shu ) ni hui zhu chong dao , chu li yi shang lei si qing kuang , jia ru bu kao lv lei de hua , shi xian dai ma shi yi yang de , zhe jiu hen zi ran di xiang dao zai bie de di fang ye neng chong yong ta men 。 zheng ru tiao kuan 41 suo shuo ming de , ji xu he mo ban ke yi yong lai she ji ke chong yong dai ma 。 zai zhe li , wo men ba liang zhong fang fa jie he qi lai shi yong , cong er man zu le ni de yao qiu 。 ni zhi yao chuang jian yi ge “ hun he feng ge ”(mixin-style) de ji lei , zhe zhong ji lei da ying zi lei ji xu ta mou yi te ding de gong neng —— zhe li zhi de shi jian li yi ge lei de new-handler de gong neng 。 zhi suo yi she ji yi ge ji lei , shi wei le rang suo you de zi lei ke yi ji xu set_new_handler he operator new gong neng , er she ji mo ban shi wei le shi mei ge zi lei you bu tong de currenthandler shu ju cheng yuan 。 zhe ting qi lai hen fu duo , bu guo ni hui kan dao dai ma qi shi hen shu xi 。 qu bie zhi bu guo shi ta xian zai ke yi bei ren he lei chong yong le 。 template // ti gong lei set_new_handler zhi chi de class newhandlersupport { // hun he feng ge ” de ji lei public: static new_handler set_new_handler(new_handler p); static void * operator new(size_t size); private: static new_handler currenthandler; }; template new_handler newhandlersupport::set_new_handler(new_handler p) { new_handler oldhandler = currenthandler; currenthandler = p; return oldhandler; } template void * newhandlersupport::operator new(size_t size) { new_handler globalhandler = std::set_new_handler(currenthandler); void *memory; try { memory = ::operator new(size); } catch (std::bad_alloc&) { std::set_new_handler(globalhandler); throw; } std::set_new_handler(globalhandler); return memory; } // this sets each currenthandler to 0 template new_handler newhandlersupport::currenthandler; you le zhe ge mo ban lei , dui lei x jia shang set_new_handler gong neng jiu hen jian dan le : zhi yao rang x cong newhandlersupport ji xu : // note inheritance from mixin base class template. (see // my article on counting objects for information on why // private inheritance might be preferable here.) class x: public newhandlersupport { ... // as before, but no declarations for }; // set_new_handler or operator new shi yong x de shi hou yi ran bu yong li hui ta mu hou zai zuo xie shen me ; lao dai ma yi ran gong zuo 。 zhe hen hao ! na xie ni chang bu qu li hui de dong xi wang wang shi zui ke xin lai de 。 shi yong set_new_handler shi chu li nei cun bu gou qing kuang xia yi zhong fang bian , jian dan de fang fa 。 zhe bi ba mei ge new dou bao zhuang zai try mo kuai li dang ran hao duo le 。 er qie , newhandlersupport zhe yang de mo ban shi de xiang ren he lei zeng jia yi ge te ding de new-handler bian de geng jian dan 。“ hun he feng ge ” de ji xu bu ke bi mian di jiang hua ti yin ru dao duo ji xu shang qu , zai zhuan dao zhe ge hua ti qian , ni yi ding yao xian yue du tiao kuan 43。 1993 nian qian ,c++ yi zhi yao qiu zai nei cun fen pei shi bai shi operator new yao fan hui 0, xian zai ze shi yao qiu operator new pao chu std::bad_alloc yi chang 。 hen duo c++ cheng xu shi zai bian yi qi kai shi zhi chi xin gui fan qian xie de 。c++ biao zhun wei yuan hui bu xiang fang qi na xie yi you de zun xun fan hui 0 gui fan de dai ma , suo yi ta men ti gong le ling wai xing shi de operator new( yi ji operator new[]—— jian tiao kuan 8) yi ji xu ti gong fan hui 0 gong neng 。 zhe xie xing shi bei cheng wei “ wu pao chu ”, yin wei ta men mei yong guo yi ge throw, er shi zai shi yong new de ru kou dian cai yong le nothrow dui xiang : class widget { ... }; widget *pw1 = new widget;// fen pei shi bai pao chu std::bad_alloc if if (pw1 == 0) ... // zhe ge jian cha yi ding shi bai widget *pw2 = new (nothrow) widget; // ruo fen pei shi bai fan hui 0 if (pw2 == 0) ... // zhe ge jian cha ke neng hui cheng gong bu guan shi yong “ zheng gui ”( ji pao chu yi chang ) xing shi de new huan shi “ wu pao chu ” xing shi de new, chong yao de shi ni bi xu wei nei cun fen pei shi bai zuo hao yu bei 。 zui jian dan de fang fa shi shi yong set_new_handler, yin wei ta dui liang zhong xing shi dou you yong 。 4. xie operator new he operator delete shi yao zun xun chang gui zi ji chong xie operator new shi ( tiao kuan 10 jie shi le wei shen me you shi yao chong xie ta ), hen chong yao de yi dian shi han shu ti gong de xing wei yao he xi tong que sheng de operator new yi zhi 。 shi ji zuo qi lai ye jiu shi : yao you zheng que de fan hui zhi ; ke yong nei cun bu gou shi yao tiao yong chu cuo chu li han shu ( jian tiao kuan 7); chu li hao 0 zi jie nei cun qing qiu de qing kuang 。 ci wai , huan yao bi mian bu xiao xin yin cang le biao zhun xing shi de new, bu guo zhe shi tiao kuan 9 de hua ti 。 you guan fan hui zhi de bu fen hen jian dan 。 jia ru nei cun fen pei qing qiu cheng gong , jiu fan hui zhi xiang nei cun de zhi zhen ; jia ru shi bai , ze zun xun tiao kuan 7 de gui ding pao chu yi ge std::bad_alloc lei xing de yi chang 。 dan shi qing ye bu shi na me jian dan 。 yin wei operator new shi ji shang hui bu zhi yi ci di chang shi zhe qu fen pei nei cun , ta yao zai mei ci shi bai hou tiao yong chu cuo chu li han shu , huan qi wang chu cuo chu li han shu neng xiang ban fa shi fang bie chu de nei cun 。 zhi you zai zhi xiang chu cuo chu li han shu de zhi zhen wei kong de qing kuang xia ,operator new cai pao chu yi chang 。 ling wai ,c++ biao zhun yao qiu , ji shi zai qing qiu fen pei 0 zi jie nei cun shi ,operator new ye yao fan hui yi ge he fa zhi zhen 。( shi ji shang , zhe ge ting qi lai guai guai de yao qiu que shi gei c++ yu yan qi ta di fang dai lai le jian bian ) zhe yang , fei lei cheng yuan xing shi de operator new de wei dai ma kan qi lai hui xiang xia mian zhe yang : void * operator new(size_t size) // operator new huan ke neng you qi ta shen shu { if (size == 0) { // chu li 0 zi jie qing qiu shi , size = 1; // ba ta dang zuo 1 ge zi jie qing qiu lai chu li } while (1) { fen pei size zi jie nei cun ; if ( fen pei cheng gong ) return ( zhi xiang nei cun de zhi zhen ); // fen pei bu cheng gong , zhao chu dang qian chu cuo chu li han shu new_handler globalhandler = set_new_handler(0); set_new_handler(globalhandler); if (globalhandler) (*globalhandler)(); else throw std::bad_alloc(); } } chu li ling zi jie qing qiu de ji qiao zai yu ba ta zuo wei qing qiu yi ge zi jie lai chu li 。 zhe kan qi lai ye hen guai , dan jian dan , he fa , you xiao 。 er qie , ni you hui duo jiu peng dao yi ci ling zi jie qing qiu de qing kuang ne ? ni you hui xi qi shang mian de wei dai ma zhong wei shen me ba chu cuo chu li han shu zhi wei 0 hou you li ji hui fu 。 zhe shi yin wei mei you ban fa ke yi zhi jie de dao chu cuo chu li han shu de zhi zhen , suo yi bi xu tong guo tiao yong set_new_handler lai zhao dao 。 ban fa hen ben dan ye you xiao 。 tiao kuan 7 ti dao operator new nei bu bao han yi ge wu xian xun huan , shang mian de dai ma qing chu di shuo ming le zhe yi dian ——while (1) jiang dao zhi wu xian xun huan 。 tiao chu xun huan de wei yi ban fa shi nei cun fen pei cheng gong huo chu cuo chu li han shu wan cheng le tiao kuan 7 suo miao shu de shi jian zhong de yi zhong : de dao le geng duo de ke yong nei cun ; an zhuang le yi ge xin de new -handler( chu cuo chu li han shu ); xie chu le new-handler; pao chu le yi ge std::bad_alloc huo qi pai sheng lei xing de yi chang ; huo zhe fan hui shi bai 。 xian zai ming bai le wei shen me new-handler bi xu zuo zhe xie gong zuo zhong de yi jian 。 jia ru bu zuo ,operator new li mian de xun huan jiu bu hui jie shu 。 hen duo ren mei you shu xi dao de yi dian shi operator new jing chang hui bei zi lei ji xu 。 zhe hui dao zhi mou xie fu duo xing 。 shang mian de wei dai ma zhong , han shu hui qu fen pei size zi jie de nei cun ( chu fei size wei 0)。size hen chong yao , yin wei ta shi chuan di gei han shu de shen shu 。 dan shi da duo shu zhen dui lei suo xie de operator new( bao kuo tiao kuan 10 zhong de na zhong ) dou shi zhi wei te ding de lei she ji de , bu shi wei suo you de lei , ye bu shi wei ta suo you de zi lei she ji de 。 zhe yi wei zhe , dui yu yi ge lei x de operator new lai shuo , han shu nei bu de xing wei zai she ji dao dui xiang de da xiao shi , dou shi jing que de sizeof(x): bu hui da ye bu hui xiao 。 dan you yu cun zai ji xu , ji lei zhong de operator new ke neng hui bei tiao yong qu wei yi ge zi lei dui xiang fen pei nei cun : class base { public: static void * operator new(size_t size); ... }; class derived: public base // derived lei mei you sheng ming operator new { ... }; // derived *p = new derived; // tiao yong base::operator new jia ru base lei de operator new bu xiang fei gong fu zhuan men qu chu li zhe zhong qing kuang —— zhe zhong qing kuang chu xian de ke neng xing bu da —— na zui jian dan de ban fa shi ba zhe ge “ cuo wu ” shu liang de nei cun fen pei qing qiu zhuan gei biao zhun operator new lai chu li , xiang xia mian zhe yang : void * base::operator new(size_t size) { if (size != sizeof(base)) // jia ru shu liang “ cuo wu ”, rang biao zhun operator new return ::operator new(size); // qu chu li zhe ge qing qiu // ... // fou ze chu li zhe ge qing qiu } “ ting !” wo ting jian ni zai jiao ,“ ni wang le jian cha yi zhong sui ran bu he li dan shi you ke neng chu xian de yi zhong qing kuang ——size you ke neng wei ling !” shi de , wo mei jian cha , dan bai tuo xia ci zai jiao chu sheng de shi hou bu yao zhe me wen zhou zhou de 。:) dan shi ji shang jian cha huan shi zuo le , zhi bu guo rong he dao size != sizeof(base) yu ju zhong le 。c++ biao zhun hen guai yi , qi zhong zhi yi jiu shi gui ding suo yi du li de (freestanding) lei de da xiao dou shi fei ling zhi 。 suo yi sizeof(base) yong yuan bu ke neng shi ling ( ji shi base lei mei you cheng yuan ), jia ru size wei ling , qing qiu hui zhuan dao ::operator new, you ta lai yi yi zhong he li de fang shi dui qing qiu jin xing chu li 。( you qu de shi , jia ru base bu shi du li de lei ,sizeof(base) you ke neng shi ling , ju ti shuo ming shen jian "my article on counting objects")。 jia ru xiang kong zhi ji yu lei de shu zu de nei cun fen pei , bi xu shi xian operator new de shu zu xing shi ——operator new[]( zhe ge han shu chang bei cheng wei “ shu zu new”, yin wei xiang bu chu "operator new[]") gai zen me fa yin )。 xie operator new[] shi , yao ji zhu ni mian dui de shi “ yuan shi ” nei cun , bu neng dui shu zu li huan bu cun zai de dui xiang jin xing ren he cao zuo 。 shi ji shang , ni shen zhi huan bu zhi dao shu zu li you duo shao ge dui xiang , yin wei bu zhi dao mei ge dui xiang you duo da 。 ji lei de operator new[] hui tong guo ji xu de fang shi bei yong lai wei zi lei dui xiang de shu zu fen pei nei cun , er zi lei dui xiang wang wang bi ji lei yao da 。 suo yi , bu neng xiang dang ran ren wei base::operator new[] li de mei ge dui xiang de da xiao dou shi sizeof(base), ye jiu shi shuo , shu zu li dui xiang de shu liang bu yi ding jiu shi ( qing qiu zi jie shu )/sizeof(base)。 guan yu operator new[] de ju ti jie shao shen jian tiao kuan m8。 chong xie operator new( he operator new[]) shi suo you yao zun xun de chang gui jiu zhe xie 。 dui yu operator delete( yi ji ta de huo ban operator delete[]), qing kuang geng jian dan 。 suo yao ji zhu de zhi shi ,c++ bao zheng shan chu kong zhi zhen yong yuan shi an quan de , suo yi ni yao chong fen di ying yong zhe yi bao zheng 。 xia mian shi fei lei cheng yuan xing shi de operator delete de wei dai ma : void operator delete(void *rawmemory) { if (rawmemory == 0) return; file:// ru / guo zhi zhen wei kong , fan hui // shi fang rawmemory zhi xiang de nei cun ; return; } zhe ge han shu de lei cheng yuan ban ben ye jian dan , zhi shi huan bi xu jian cha bei shan chu de dui xiang de da xiao 。 jia she lei de operator new jiang “ cuo wu ” da xiao de fen pei qing qiu zhuan gei ::operator new, na me ye bi xu jiang “ cuo wu ” da xiao de shan chu qing qiu zhuan gei ::operator delete: class base { // he qian mian yi yang , zhi shi zhe li sheng ming le public: // operator delete static void * operator new(size_t size); static void operator delete(void *rawmemory, size_t size); ... }; void base::operator delete(void *rawmemory, size_t size) { if (rawmemory == 0) return; // jian cha kong zhi zhen if (size != sizeof(base)) { // jia ru size" cuo wu ", ::operator delete(rawmemory); // rang biao zhun operator lai chu li qing qiu return; } shi fang zhi xiang rawmemory de nei cun ; return; } ke jian , you guan operator new he operator delete( yi ji ta men de shu zu xing shi ) de gui ding bu shi na me ma fan , chong yao de shi bi xu zun shou ta 。 zhi yao nei cun fen pei cheng xu zhi chi new-handler han shu bing zheng que di chu li le ling nei cun qing qiu , jiu cha bu duo le ; jia ru nei cun shi fang cheng xu you chu li le kong zhi zhen , na jiu mei qi ta shen me yao zuo de le 。 zhi yu zai lei cheng yuan ban ben de han shu li zeng jia ji xu zhi chi , na jiang hen kuai jiu ke yi wan cheng 。 5. bi mian yin cang biao zhun xing shi de new              yin wei nei bu fan wei sheng ming de ming cheng hui yin cang diao wai bu fan wei de xiang tong de ming cheng , suo yi dui yu fen bie zai lei de nei bu he quan ju sheng ming de liang ge xiang tong ming zi de han shu f lai shuo , lei de cheng yuan han shu hui yin cang diao quan ju han shu : void f(); // quan ju han shu class x { public: void f(); // cheng yuan han shu }; x x; f(); // tiao yong f x.f(); // tiao yong x::f zhe bu hui ling ren jing qi , ye bu hui dao zhi hun xiao , yin wei tiao yong quan ju han shu he cheng yuan han shu shi zong shi cai yong bu tong de yu fa xing shi 。 ran er jia ru ni zai lei li zeng jia le yi ge dai duo ge shen shu de operator new han shu , jie guo jiu you ke neng ling ren da chi yi jing 。 class x { public: void f(); // operator new de shen shu zhi ding yi ge // new-hander(new de chu cuo chu li ) han shu static void * operator new(size_t size, new_handler p); }; void specialerrorhandler(); // ding yi zai bie de di fang x *px1 = new (specialerrorhandler) x; // tiao yong x::operator new x *px2 = new x; // cuo wu ! zai lei li ding yi le yi ge cheng wei “operator new” de han shu hou , hui bu jing yi di zu zhi le dui biao zhun new de fang wen 。 tiao kuan 50 jie shi le wei shen me hui zhe yang , zhe li wo men geng guan xin de shi ru he xiang ge ban fa bi mian zhe ge wen ti 。 yi ge ban fa shi zai lei li xie yi ge zhi chi biao zhun new tiao yong fang shi de operator new, ta he biao zhun new zuo tong yang de shi 。 zhe ke yi yong yi ge gao xiao de nei lian han shu lai feng zhuang shi xian 。 class x { public: void f(); static void * operator new(size_t size, new_handler p); static void * operator new(size_t size) { return ::operator new(size); } }; x *px1 = new (specialerrorhandler) x; // tiao yong x::operator // new(size_t, new_handler) x* px2 = new x; // tiao yong x::operator // new(size_t) ling yi zhong fang fa shi wei mei yi ge zeng jia dao operator new de shen shu ti gong que sheng zhi ( jian tiao kuan 24): class x { public: void f(); static void * operator new(size_t size, // p que sheng zhi wei 0 new_handler p = 0); // }; x *px1 = new (specialerrorhandler) x; // zheng que x* px2 = new x; // ye zheng que wu lun na zhong fang fa , jia ru yi hou xiang dui “ biao zhun ” xing shi de new ding zhi xin de gong neng , zhi xu yao chong xie zhe ge han shu 。 tiao yong zhe chong xin bian yi lian jie hou jiu ke yi shi yong xin gong neng le 。 6. jia ru xie le operator new jiu yao tong shi xie operator delete rang wo men hui guo tou qu kan kan zhe yang yi ge ji ben wen ti : wei shen me you bi yao xie zi ji de operator new he operator delete? da an tong chang shi : wei le xiao lv 。 que sheng de operator new he operator delete ju you fei chang hao de tong yong xing , ta de zhe zhong ling huo xing ye shi de zai mou xie te ding de chang he xia , ke yi jin yi bu gai shan ta de xing neng 。 you qi zai na xie xu yao dong tai fen pei da liang de dan hen xiao de dui xiang de ying yong cheng xu li , qing kuang geng shi ru ci 。 li ru you zhe yang yi ge biao shi fei ji de lei : lei airplane zhi bao han yi ge zhi zhen , ta zhi xiang de shi fei ji dui xiang de shi ji miao shu ( ci ji shu zai tiao kuan 34 jin xing shuo ming ): class airplanerep { ... }; // biao shi yi ge fei ji dui xiang // class airplane { public: ... private: airplanerep *rep; // zhi xiang shi ji miao shu }; yi ge airplane dui xiang bing bu da , ta zhi bao han yi ge zhi zhen ( zheng ru tiao kuan 14 he m24 suo shuo ming de , jia ru airplane lei sheng ming le xu han shu , hui yin shi bao han di er ge zhi zhen )。 dan dang tiao yong operator new lai fen pei yi ge airplane dui xiang shi , de dao de nei cun ke neng yao bi cun chu zhe ge zhi zhen ( huo yi dui zhi zhen ) suo xu yao de yao duo 。 zhi suo yi hui chan sheng zhe zhong kan qi lai hen xi qi de xing wei , zai yu operator new he operator delete zhi jian xu yao hu xiang chuan di xin xi 。 yin wei que sheng ban ben de operator new shi yi zhong tong yong xing de nei cun fen pei qi , ta bi xu ke yi fen pei ren yi da xiao de nei cun kuai 。 tong yang ,operator delete ye yao ke yi shi fang ren yi da xiao de nei cun kuai 。operator delete xiang nong qing ta yao shi fang de nei cun you duo da , jiu bi xu zhi dao dang chu operator new fen pei de nei cun you duo da 。 you yi zhong chang yong de fang fa ke yi rang operator new lai gao su operator delete dang chu fen pei de nei cun da xiao shi duo shao , jiu shi zai ta suo fan hui de nei cun li yu xian fu dai yi xie e wai xin xi , yong lai zhi ming bei fen pei de nei cun kuai de da xiao 。 ye jiu shi shuo , dang ni xie le xia mian de yu ju , airplane *pa = new airplane; ni bu hui de dao yi kuai kan qi lai xiang zhe yang de nei cun kuai : pa——> airplane dui xiang de nei cun er shi de dao xiang zhe yang de nei cun kuai : pa——> nei cun kuai da xiao shu ju + airplane dui xiang de nei cun dui yu xiang airplane zhe yang hen xiao de dui xiang lai shuo , zhe xie e wai de shu ju xin xi hui shi de dong tai fen pei dui xiang shi suo xu yao de de nei cun de da xiao fan fan ( fei fan shi lei li mei you xu ni han shu de shi hou )。 jia ru ruan jian yun xing zai yi ge nei cun hen bao gui de huan jing zhong , jiu cheng shou bu qi zhe zhong she chi de nei cun fen pei fang an le 。 wei airplane lei zhuan men xie yi ge operator new, jiu ke yi li yong mei ge airplane de da xiao dou xiang deng de te dian , bu bi zai mei ge fen pei de nei cun kuai shang jia shang fu dai xin xi le 。 ju ti lai shuo , you zhe yang yi ge fang fa lai shi xian ni de zi ding yi de operator new: xian rang que sheng operator new fen pei yi xie da kuai de yuan shi nei cun , mei kuai de da xiao dou zu yi rong na hen duo ge airplane dui xiang 。airplane dui xiang de nei cun kuai jiu qu zi zhe xie da de nei cun kuai 。 dang qian mei bei shi yong de nei cun kuai bei zu zhi cheng lian biao —— cheng wei zi you lian biao —— yi bei wei lai airplane shi yong 。 ting qi lai hao xiang mei ge dui xiang dou yao cheng dan yi ge next yu de kai xiao ( yong yu zhi chi lian biao ), dan bu hui :rep yu de kong jian ye bei yong lai cun chu next zhi zhen ( yin wei zhi shi zuo wei airplane dui xiang lai shi yong de nei cun kuai cai xu yao rep zhi zhen ; tong yang , zhi you mei zuo wei airplane dui xiang shi yong de nei cun kuai cai xu yao next zhi zhen ), zhe ke yi yong union lai shi xian 。 ju ti shi xian shi , jiu yao xiu gai airplane de ding yi , cong er zhi chi zi ding yi de nei cun zhi li 。 ke yi zhe me zuo : class airplane { // xiu gai hou de lei — zhi chi zi ding yi de nei cun zhi li public: // static void * operator new(size_t size); ... private: union { airplanerep *rep; // yong yu bei shi yong de dui xiang airplane *next; // yong yu mei bei shi yong de ( zai zi you lian biao zhong ) dui xiang }; // lei de chang liang , zhi ding yi ge da de nei cun kuai zhong fang duo shao ge // airplane dui xiang , zai hou mian chu shi hua static const int block_size; static airplane *headoffreelist; }; shang mian de dai ma zeng jia le de ji ge sheng ming : yi ge operator new han shu , yi ge lian he ( shi de rep he next yu zhan yong tong yang de kong jian ), yi ge chang liang ( zhi ding da nei cun kuai de da xiao ), yi ge jing tai zhi zhen ( gen zong zi you lian biao de biao tou )。 biao tou zhi zhen sheng ming wei jing tai cheng yuan hen chong yao , yin wei zheng ge lei zhi you yi ge zi you lian biao , er bu shi mei ge airplane dui xiang dou you 。 xia mian gai xie operator new han shu le : void * airplane::operator new(size_t size) { // ba “ cuo wu ” da xiao de qing qiu zhuan gei ::operator new() chu li ; // xiang jian tiao kuan 8 if (size != sizeof(airplane)) return ::operator new(size); airplane *p = // p zhi xiang zi you lian biao de biao tou headoffreelist; // // p ruo he fa , ze jiang biao tou yi dong dao ta de xia yi ge yuan su // if (p) headoffreelist = p->next; else { // zi you lian biao wei kong , ze fen pei yi ge da de nei cun kuai , // ke yi rong na block_size ge airplane dui xiang airplane *newblock = static_cast(::operator new(block_size * sizeof(airplane))); // jiang mei ge xiao nei cun kuai lian jie qi lai xing cheng yi ge xin de zi you lian biao // tiao guo di 0 ge yuan su , yin wei ta yao bei fan hui gei operator new de tiao yong zhe // for (int i = 1; i < block_size-1; ++i) newblock[i].next = &newblock[i+1]; // yong kong zhi zhen jie shu lian biao newblock[block_size-1].next = 0; // p she wei biao de tou bu ,headoffreelist zhi xiang de // nei cun kuai jin gen qi hou p = newblock; headoffreelist = &newblock[1]; } return p; } jia ru ni du le tiao kuan 8, jiu hui zhi dao zai operator new bu neng man zu nei cun fen pei qing qiu shi , hui zhi xing yi xi lie yu new-handler han shu he li wai you guan de li xing xing dong zuo 。 shang mian de dai ma mei you zhe xie bu zhou , zhe shi yin wei operator new zhi li de nei cun dou shi cong ::operator new fen pei lai de 。 zhe yi wei zhe zhi you ::operator new shi bai shi ,operator new cai hui shi bai 。 er jia ru ::operator new shi bai , ta hui qu zhi xing new-handler de dong zuo ( ke neng zui hou yi pao chu yi chang jie shu ), suo yi bu xu yao airplane de operator new ye qu chu li 。 huan ju hua shuo , qi shi new-handler de dong zuo dou huan zai , ni zhi shi mei kan jian , ta yin cang zai ::operator new li 。 you le operator new, xia mian yao zuo de jiu shi gei chu airplane de jing tai shu ju cheng yuan de ding yi : airplane *airplane::headoffreelist; const int airplane::block_size = 512; mei bi yao xian shi di jiang headoffreelist she zhi wei kong zhi zhen , yin wei jing tai cheng yuan de chu shi zhi dou bei que sheng she wei 0。block_size jue ding le yao cong ::operator new huo de duo da de nei cun kuai 。 zhe ge ban ben de operator new jiang hui gong zuo de fei chang hao 。 ta wei airplane dui xiang fen pei de nei cun yao bi que sheng operator new geng shao , er qie yun xing de geng kuai , ke neng hui kuai 2 ci fang de deng ji 。 zhe mei shen me xi qi de , tong yong xing de que sheng operator new bi xu ying fu ge zhong da xiao de nei cun qing qiu , huan yao chu li nei bu wai bu de sui pian ; er ni de operator new zhi yong cao zuo lian biao zhong de yi dui zhi zhen 。 pao qi ling huo xing wang wang ke yi hen qing yi di huan lai su du 。 xia mian wo men jiang tao lun operator delete。 huan ji de operator delete ma ? ben tiao kuan jiu shi guan yu operator delete de tao lun 。 dan zhi dao xian zai wei zhi ,airplane lei zhi sheng ming le operator new, huan mei sheng ming operator delete。 xiang xiang jia ru xie le xia mian de dai ma hui fa sheng shen me : airplane *pa = new airplane; // tiao yong // airplane::operator new ... delete pa; // tiao yong ::operator delete du zhe duan dai ma shi , jia ru ni shu qi er duo , hui ting dao fei ji zhuang hui ran shao de sheng yin , huan you cheng xu yuan de ku qi 。 wen ti chu zai operator new( zai airplane li ding yi de na ge ) fan hui le yi ge bu dai tou xin xi de nei cun de zhi zhen , er operator delete( que sheng de na ge ) que jia she chuan gei ta de nei cun bao han tou xin xi 。 zhe jiu shi bei ju chan sheng de yuan yin 。 zhe ge li zi shuo ming le yi ge pu bian yuan ze :operator new he operator delete bi xu tong shi xie , zhe yang cai bu hui chu xian bu tong de jia she 。 jia ru xie le yi ge zi ji de nei cun fen pei cheng xu , jiu yao tong shi xie yi ge shi fang cheng xu 。( guan yu wei shen me yao zun xun zhe tiao gui ding de ling yi ge li you , shen jian article on counting objects yi wen de the sidebar on placement zhang jie ) yin er , ji xu she ji airplane lei ru xia : class airplane { // he qian mian de yi yang , zhi bu guo zeng jia le yi ge public: // operator delete de sheng ming ... static void operator delete(void *deadobject, size_t size); }; // chuan gei operator delete de shi yi ge nei cun kuai , jia ru // qi da xiao zheng que , jiu jia dao zi you nei cun kuai lian biao de zui qian mian // void airplane::operator delete(void *deadobject, size_t size) { if (deadobject == 0) return; // jian tiao kuan 8 if (size != sizeof(airplane)) { // jian tiao kuan 8 ::operator delete(deadobject); return; } airplane *carcass = static_cast(deadobject); carcass->next = headoffreelist; headoffreelist = carcass; } yin wei qian mian zai operator new li jiang “ cuo wu ” da xiao de qing qiu zhuan gei le quan ju operator new( jian tiao kuan 8), na me zhe li tong yang yao jiang “ cuo wu ” da xiao de dui xiang jiao gei quan ju operator delete lai chu li 。 jia ru bu zhe yang , jiu hui chong xian ni qian mian fei jin xin si xiang bi mian de na zhong wen ti ——new he delete ju fa shang de bu pi pei 。 you qu de shi , jia ru yao shan chu de dui xiang shi cong yi ge mei you xu xi gou han shu de lei ji xu er lai de , na chuan gei operator delete de size_t zhi you ke neng bu zheng que 。 zhe jiu shi bi xu bao zheng ji lei bi xu yao you xu xi gou han shu de yuan yin , ci wai tiao kuan 14 huan lie chu le di er ge 、 li you geng chong zu de yuan yin 。 zhe li zhi yao jian dan di ji zhu , ji lei jia ru yi lou le xu ni gou han shu ,operator delete jiu you ke neng gong zuo bu zheng que 。 suo you yi qie dou hen hao , dan cong ni zhou qi de mei tou wo ke yi zhi dao ni yi ding zai dan xin nei cun xie lu 。 you zhe da liang kai fa jing yan de ni bu hui mei zhu chong dao ,airplane de operator new tiao yong ::operator new de dao le da kuai nei cun , dan airplane de operator delete que mei you shi fang ta men 。 nei cun xie lu ! nei cun xie lu ! wo fen ming ting jian le jing zhong zai ni nao hai li hui xiang 。 dan qing zai xi ting wo hui da , zhe li mei you nei cun xie lu ! yin qi nei cun xie lu de yuan yin zai yu nei cun fen pei hou zhi xiang nei cun de zhi zhen diu shi le 。 jia ru mei you la ji chu li huo qi ta yu yan zhi wai de ji zhi , zhe xie nei cun jiu bu hui bei shou hui 。 dan shang mian de she ji mei you nei cun xie lu , yin wei ta jue bu hui chu xian nei cun zhi zhen diu shi de qing kuang 。 mei ge da nei cun kuai shou xian bei fen cheng airplane da xiao de xiao kuai , ran hou zhe xie xiao kuai bei fang zai zi you lian biao shang 。 dang ke hu tiao yong airplane::operator new shi , xiao kuai bei zi you lian biao yi chu , ke hu de dao zhi xiang xiao kuai de zhi zhen 。 dang ke hu tiao yong operator delete shi , xiao kuai bei fang hui dao zi you lian biao shang 。 cai yong zhe zhong she ji , suo you de nei cun kuai yao bu bei airplane dui xiang shi yong ( zhe zhong qing kuang xia , shi you ke hu lai fu ze bi mian nei cun xie lu ), yao bu jiu zai zi you lian biao shang ( zhe zhong qing kuang xia nei cun kuai you zhi zhen )。 suo yi shuo zhe li mei you nei cun xie lu 。 ran er que shi ,::operator new fan hui de nei cun kuai shi cong lai mei you bei airplane::operator delete shi fang , zhe ge nei cun kuai you ge ming zi , jiao nei cun chi 。 dan nei cun xie lou he nei cun chi you yi ge chong yao de bu tong zhi chu 。 nei cun xie lou hui wu xian di zeng chang , ji shi ke hu xun gui dao ju ; er nei cun chi de da xiao jue bu hui chao guo ke hu qing qiu nei cun de zui da zhi 。 xiu gai airplane de nei cun zhi li cheng xu shi de ::operator new fan hui de nei cun kuai zai bu bei shi yong shi zi dong shi fang bing bu nan , dan zhe li bu hui zhe me zuo , zhe you liang ge yuan yin : di yi ge yuan yin he ni zi ding yi nei cun zhi li de chu zhong you guan 。 ni you hen duo li you qu zi ding yi nei cun zhi li , zui ji ben de yi tiao shi ni que ren que sheng de operator new he operator delete shi yong le tai duo de nei cun huo ( bing qie ) yun xing hen man 。 he cai yong nei cun chi ce lue xiang bi , gen zong he shi fang na xie da nei cun kuai suo xie de mei yi ge e wai de zi jie he mei yi tiao e wai de yu ju dou hui dao zhi ruan jian yun xing geng man , yong de nei cun geng duo 。 zai she ji xing neng yao qiu hen gao de ku huo cheng xu shi , jia ru ni yu ji nei cun chi de da xiao hui zai yi ge he li de fan wei zhi nei , na cai yong nei cun chi de fang fa zai hao bu guo le 。 di er ge yuan yin he chu li yi xie bu he li de cheng xu xing wei you guan 。 jia she airplane de nei cun zhi li cheng xu bei xiu gai le ,airplane de operator delete ke yi shi fang ren he mei you dui xiang cun zai de da kuai de nei cun 。 na kan xia mian de cheng xu : int main() { airplane *pa = new airplane; // di yi ci fen pei : de dao da kuai nei cun , // sheng cheng zi you lian biao , deng delete pa; // nei cun kuai kong ; // shi fang ta pa = new airplane; // zai ci de dao da kuai nei cun , // sheng cheng zi you lian biao , deng delete pa; // nei cun kuai zai ci kong , // shi fang ... // ni you le xiang fa ... return 0; } zhe ge zao gao de xiao cheng xu hui bi yong que sheng de operator new he operator delete xie de cheng xu yun xing de huan man , zhan yong huan yao duo de nei cun , geng bu yao he yong nei cun chi xie de cheng xu bi le 。 dang ran you ban fa chu li zhe zhong bu he li de qing kuang , dan kao lv de fei fan qing kuang yue duo , jiu yue you ke neng yao chong xin shi xian nei cun zhi li han shu , er zui hou ni you hui de dao shen me ne ? nei cun chi bu neng jie jue suo you de nei cun zhi li wen ti , zai hen duo qing kuang xia shi hen shi he de 。 shi ji kai fa zhong , ni hui jing chang yao gei xu duo bu tong de lei shi xian ji yu nei cun chi de gong neng 。 ni hui xiang ,“ yi ding you shen me ban fa ba zhe zhong gu ding da xiao nei cun de fen pei qi feng zhuang qi lai , cong er ke yi fang bian di shi yong ”。 shi de , you ban fa 。 sui ran wo zai zhe ge tiao kuan yi jing lao tao zhe me chang shi jian le , dan huan shi yao jian dan jie shao yi xia , ju ti shi xian liu gei du zhe zuo lian xi 。 xia mian jian dan gei chu le yi ge pool lei de zui xiao jie kou ( jian tiao kuan 18),pool lei de mei ge dui xiang shi mou lei dui xiang ( qi da xiao zai pool de gou zao han shu li zhi ding ) de nei cun fen pei qi 。 class pool { public: pool(size_t n); // wei da xiao wei n de dui xiang chuang jian // yi ge fen pei qi void * alloc(size_t n) ; // wei yi ge dui xiang fen pei zu gou nei cun // zun xun tiao kuan 8 de operator new chang gui void free( void *p, size_t n); // jiang p suo zhi de nei cun fan hui dao nei cun chi ; // zun xun tiao kuan 8 de operator delete chang gui ~pool(); // shi fang nei cun chi zhong quan bu nei cun }; zhe ge lei zhi chi pool dui xiang de chuang jian , zhi xing fen pei he shi fang cao zuo , yi ji bei cui hui 。pool dui xiang bei cui hui shi , hui shi fang ta fen pei de suo you nei cun 。 zhe jiu shi shuo , xian zai you ban fa bi mian airplane de han shu li suo biao xian de nei cun xie lou si de xing wei le 。 ran er zhe ye yi wei zhe , jia ru pool de xi gou han shu tiao yong tai kuai ( shi yong nei cun chi de dui xiang mei you quan bu bei cui hui ), yi xie dui xiang jiu hui fa xian ta zheng zai shi yong de nei cun meng ran jian mei le 。 zhe zao cheng de jie guo tong chang shi bu ke cai ce de 。 you le zhe ge pool lei , ji shi Java cheng xu yuan ye ke yi bu fei chui hui zhi li di zai airplane lei li zeng jia zi ji de nei cun zhi li gong neng : class airplane { public: ... // pu tong airplane gong neng static void * operator new(size_t size); static void operator delete(void *p, size_t size); private: airplanerep *rep; // zhi xiang shi ji miao shu de zhi zhen static pool mempool; // airplanes de nei cun chi }; inline void * airplane::operator new(size_t size) { return mempool.alloc(size); } inline void airplane::operator delete(void *p, size_t size) { mempool.free(p, size); } // wei airplane dui xiang chuang jian yi ge nei cun chi , // zai lei de shi xian wen jian li shi xian pool airplane::mempool(sizeof(airplane)); zhe ge she ji bi qian mian de yao qing chu 、 gan jing de duo , yin wei airplane lei bu zai he fei airplane de dai ma hun zai yi qi 。union, zi you lian biao tou zhi zhen , ding yi yuan shi nei cun kuai da xiao de chang liang dou bu jian le , ta men dou yin cang zai ta men ying gai dai de di fang ——pool lei li 。 rang xie pool de cheng xu yuan qu cao xin nei cun zhi li de xi jie ba , ni de gong zuo zhi shi rang airplane lei zheng chang gong zuo 。 xian zai ying gai ming bai le , zi ding yi de nei cun zhi li cheng xu ke yi hen hao di gai shan cheng xu de xing neng , er qie ta men ke yi feng zhuang zai xiang pool zhe yang de lei li 。 dan qing bu yao wang ji zhu yao de yi dian ,operator new he operator delete xu yao tong shi gong zuo , na me ni xie le operator new, jiu ye yi ding yao xie operator delete。 ban quan wen ti :Effective c++ second edition zhong wen ban
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
 
©2005- 王朝网络 版权所有