| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> c/c++ >> gao zhi liang C++/C bian cheng zhi nan ( qi )
 

gao zhi liang C++/C bian cheng zhi nan ( qi )

2008-06-01 02:07:23 编辑來源:互联网 评论
 
本文为【高质量C++/C编程指南(七)】的拼音翻译版
  di 7 zhang nei cun zhi li
  huan ying jin ru nei cun zhe pian lei qu 。 wei da de Bill Gates zeng jing shi yan :
  640K ought to be enough for everybody
  — Bill Gates 1981
  cheng xu yuan men jing chang bian xie nei cun zhi li cheng xu , wang wang ti xin diao dan 。 jia ru bu xiang chu lei , wei yi de jie jue ban fa jiu shi fa xian suo you qian fu de di lei bing qie pai chu ta men , duo shi duo bu le de 。 ben zhang de nei rong bi yi ban jiao ke shu de yao shen ru de duo , du zhe xu xi xin yue du , zuo dao zhen zheng di tong xiao nei cun zhi li 。
  7.1 nei cun fen pei fang shi
  nei cun fen pei fang shi you san zhong :
  (1) cong jing tai cun chu qu yu fen pei 。 nei cun zai cheng xu bian yi de shi hou jiu yi jing fen pei hao , zhe kuai nei cun zai cheng xu de zheng ge yun xing qi jian dou cun zai 。 li ru quan ju bian liang ,static bian liang 。
  (2) zai zhan shang chuang jian 。 zai zhi xing han shu shi , han shu nei ju bu bian liang de cun chu dan yuan dou ke yi zai zhan shang chuang jian , han shu zhi xing jie shu shi zhe xie cun chu dan yuan zi dong bei shi fang 。 zhan nei cun fen pei yun suan nei zhi yu chu li qi de zhi ling ji zhong , xiao lv hen gao , dan shi fen pei de nei cun rong liang you xian 。
  (3) cong dui shang fen pei , yi cheng dong tai nei cun fen pei 。 cheng xu zai yun xing de shi hou yong malloc huo new shen qing ren yi duo shao de nei cun , cheng xu yuan zi ji fu ze zai he shi yong free huo delete shi fang nei cun 。 dong tai nei cun de sheng cun qi you wo men jue ding , shi yong fei chang ling huo , dan wen ti ye zui duo 。
  7.2 chang jian de nei cun cuo wu ji qi dui ce
  fa sheng nei cun cuo wu shi jian fei chang ma fan de shi qing 。 bian yi qi bu neng zi dong fa xian zhe xie cuo wu , tong chang shi zai cheng xu yun xing shi cai neng bu zhuo dao 。 er zhe xie cuo wu da duo mei you ming xian de zheng zhuang , shi yin shi xian , zeng jia le gai cuo de nan du 。 you shi yong hu nu qi chong chong di ba ni zhao lai , cheng xu que mei you fa sheng ren he wen ti , ni yi zou , cuo wu you fa zuo le 。
  chang jian de nei cun cuo wu ji qi dui ce ru xia :
  u nei cun fen pei wei cheng gong , que shi yong le ta 。
  bian cheng xin shou chang fan zhe zhong cuo wu , yin wei ta men mei you yi shi dao nei cun fen pei hui bu cheng gong 。 chang yong jie jue ban fa shi , zai shi yong nei cun zhi qian jian cha zhi zhen shi fou wei NULL。 jia ru zhi zhen p shi han shu de shen shu , na me zai han shu de ru kou chu yong assert(p!=NULL) jin xing jian cha 。 jia ru shi yong malloc huo new lai shen qing nei cun , ying gai yong if(p==NULL) huo if(p!=NULL) jin xing fang cuo chu li 。
  u nei cun fen pei sui ran cheng gong , dan shi shang wei chu shi hua jiu yin yong ta 。
  fan zhe zhong cuo wu zhu yao you liang ge qi yin : yi shi mei you chu shi hua de guan nian ; er shi wu yi wei nei cun de que sheng chu zhi quan wei ling , dao zhi yin yong chu zhi cuo wu ( li ru shu zu )。
  nei cun de que sheng chu zhi jiu jing shi shen me bing mei you tong yi de biao zhun , jin guan you xie shi hou wei ling zhi , wo men ning ke xin qi wu bu ke xin qi you 。 suo yi wu lun yong he zhong fang shi chuang jian shu zu , dou bie wang le fu chu zhi , ji bian shi fu ling zhi ye bu ke sheng lue , bu yao xian ma fan 。
  u nei cun fen pei cheng gong bing qie yi jing chu shi hua , dan cao zuo yue guo le nei cun de bian jie 。
  li ru zai shi yong shu zu shi jing chang fa sheng xia biao “ duo 1” huo zhe “ shao 1” de cao zuo 。 fei fan shi zai for xun huan yu ju zhong , xun huan ci shu hen qing yi gao cuo , dao zhi shu zu cao zuo yue jie 。
  u wang ji le shi fang nei cun , zao cheng nei cun xie lu 。
  han you zhe zhong cuo wu de han shu mei bei tiao yong yi ci jiu diu shi yi kuai nei cun 。 gang kai shi shi xi tong de nei cun chong zu , ni kan bu dao cuo wu 。 zhong you yi ci cheng xu hu ran si diao , xi tong chu xian ti shi : nei cun hao jin 。
  dong tai nei cun de shen qing yu shi fang bi xu pei dui , cheng xu zhong malloc yu free de shi yong ci shu yi ding yao xiang tong , fou ze ken ding you cuo wu (new/delete tong li )。
  u shi fang le nei cun que ji xu shi yong ta 。
  you san zhong qing kuang :
  (1) cheng xu zhong de dui xiang tiao yong guan xi guo yu fu duo , shi zai nan yi gao qing chu mou ge dui xiang jiu jing shi fou yi jing shi fang le nei cun , ci shi ying gai chong xin she ji shu ju jie gou , cong gen ben shang jie jue dui xiang zhi li de hun luan ju mian 。
  (2) han shu de return yu ju xie cuo le , zhu chong bu yao fan hui zhi xiang “ zhan nei cun ” de “ zhi zhen ” huo zhe “ yin yong ”, yin wei gai nei cun zai han shu ti jie shu shi bei zi dong xiao hui 。
  (3) shi yong free huo delete shi fang le nei cun hou , mei you jiang zhi zhen she zhi wei NULL。 dao zhi chan sheng “ ye zhi zhen ”。
  l 【 gui ze 7-2-1】 yong malloc huo new shen qing nei cun zhi hou , ying gai li ji jian cha zhi zhen zhi shi fou wei NULL。 fang zhi shi yong zhi zhen zhi wei NULL de nei cun 。
  l 【 gui ze 7-2-2】 bu yao wang ji wei shu zu he dong tai nei cun fu chu zhi 。 fang zhi jiang wei bei chu shi hua de nei cun zuo wei you zhi shi yong 。
  l 【 gui ze 7-2-3】 bi mian shu zu huo zhi zhen de xia biao yue jie , fei fan yao dang xin fa sheng “ duo 1” huo zhe “ shao 1” cao zuo 。
  l 【 gui ze 7-2-4】 dong tai nei cun de shen qing yu shi fang bi xu pei dui , fang zhi nei cun xie lou 。
  l 【 gui ze 7-2-5】 yong free huo delete shi fang le nei cun zhi hou , li ji jiang zhi zhen she zhi wei NULL, fang zhi chan sheng “ ye zhi zhen ”。
  7.3 zhi zhen yu shu zu de dui bi
  C++/C cheng xu zhong , zhi zhen he shu zu zai bu shao di fang ke yi xiang hu ti huan zhe yong , rang ren chan sheng yi zhong cuo jiao , yi wei liang zhe shi deng jia de 。
  shu zu yao me zai jing tai cun chu qu bei chuang jian ( ru quan ju shu zu ), yao me zai zhan shang bei chuang jian 。 shu zu ming dui ying zhe ( er bu shi zhi xiang ) yi kuai nei cun , qi di zhi yu rong liang zai sheng ming qi nei bao chi bu bian , zhi you shu zu de nei rong ke yi gai bian 。
  zhi zhen ke yi sui shi zhi xiang ren yi lei xing de nei cun kuai , ta de te zheng shi “ ke bian ”, suo yi wo men chang yong zhi zhen lai cao zuo dong tai nei cun 。 zhi zhen yuan bi shu zu ling huo , dan ye geng wei xian 。
  xia mian yi zi fu chuan wei li bi jiao zhi zhen yu shu zu de te xing 。
  7.3.1 xiu gai nei rong
  shi li 7-3-1 zhong , zi fu shu zu a de rong liang shi 6 ge zi fu , qi nei rong wei hello\0。a de nei rong ke yi gai bian , ru a[0]= ‘X’。 zhi zhen p zhi xiang chang liang zi fu chuan “world”( wei yu jing tai cun chu qu , nei rong wei world\0), chang liang zi fu chuan de nei rong shi bu ke yi bei xiu gai de 。 cong yu fa shang kan , bian yi qi bing bu jiao de yu ju p[0]= ‘X’ you shen me bu tuo , dan shi gai yu ju qi tu xiu gai chang liang zi fu chuan de nei rong er dao zhi yun xing cuo wu 。
  char a[] = “hello”;
  a[0] = ‘X’;
  cout << a << endl;
  char *p = “world”; // zhu chong p zhi xiang chang liang zi fu chuan
  p[0] = ‘X’; // bian yi qi bu neng fa xian gai cuo wu
  cout << p << endl;
  
   shi li 7-3-1 xiu gai shu zu he zhi zhen de nei rong
  7.3.2 nei rong fu zhi yu bi jiao
  bu neng dui shu zu ming jin xing zhi jie fu zhi yu bi jiao 。 shi li 7-3-2 zhong , ruo xiang ba shu zu a de nei rong fu zhi gei shu zu b, bu neng yong yu ju b = a , fou ze jiang chan sheng bian yi cuo wu 。 ying gai yong biao zhun ku han shu strcpy jin xing fu zhi 。 tong li , bi jiao b he a de nei rong shi fou xiang tong , bu neng yong if(b==a) lai pan ding , ying gai yong biao zhun ku han shu strcmp jin xing bi jiao 。
  yu ju p = a bing bu neng ba a de nei rong fu zhi zhi zhen p, er shi ba a de di zhi fu gei le p。 yao xiang fu zhi a de nei rong , ke yi xian yong ku han shu malloc wei p shen qing yi kuai rong liang wei strlen(a)+1 ge zi fu de nei cun , zai yong strcpy jin xing zi fu chuan fu zhi 。 tong li , yu ju if(p==a) bi jiao de bu shi nei rong er shi di zhi , ying gai yong ku han shu strcmp lai bi jiao 。
  // shu zu …
  char a[] = "hello";
  char b[10];
  strcpy(b, a); // bu neng yong b = a;
  if(strcmp(b, a) == 0) // bu neng yong if (b == a)
  …
  // zhi zhen …
  int len = strlen(a);
  char *p = (char *)malloc(sizeof(char)*(len+1));
  strcpy(p,a); // bu yao yong p = a;
  if(strcmp(p, a) == 0) // bu yao yong if (p == a)
  …
  shi li 7-3-2 shu zu he zhi zhen de nei rong fu zhi yu bi jiao
  7.3.3 ji suan nei cun rong liang
  yong yun suan fu sizeof ke yi ji suan chu shu zu de rong liang ( zi jie shu )。 shi li 7-3-3(a) zhong ,sizeof(a) de zhi shi 12( zhu chong bie wang le ’\0’)。 zhi zhen p zhi xiang a, dan shi sizeof(p) de zhi que shi 4。 zhe shi yin wei sizeof(p) de dao de shi yi ge zhi zhen bian liang de zi jie shu , xiang dang yu sizeof(char*), er bu shi p suo zhi de nei cun rong liang 。C++/C yu yan mei you ban fa zhi dao zhi zhen suo zhi de nei cun rong liang , chu fei zai shen qing nei cun shi ji zhu ta 。
  zhu chong dang shu zu zuo wei han shu de shen shu jin xing chuan di shi , gai shu zu zi dong tui hua wei tong lei xing de zhi zhen 。 shi li 7-3-3(b) zhong , bu lun shu zu a de rong liang shi duo shao ,sizeof(a) shi zhong deng yu sizeof(char *)。
  char a[] = "hello world";
  char *p = a;
  cout<< sizeof(a) << endl; // 12 zi jie
  cout<< sizeof(p) << endl; // 4 zi jie
  shi li 7-3-3(a) ji suan shu zu he zhi zhen de nei cun rong liang
  void Func(char a[100])
  {
  cout<< sizeof(a) << endl; // 4 zi jie er bu shi 100 zi jie
  }
  shi li 7-3-3(b) shu zu tui hua wei zhi zhen
  7.4 zhi zhen shen shu shi ru he chuan di nei cun de ?
  jia ru han shu de shen shu shi yi ge zhi zhen , bu yao zhi wang yong gai zhi zhen qu shen qing dong tai nei cun 。 shi li 7-4-1 zhong ,Test han shu de yu ju GetMemory(str, 200) bing mei you shi str huo de qi wang de nei cun ,str yi jiu shi NULL, wei shen me ?
  void GetMemory(char *p, int num)
  {
  p = (char *)malloc(sizeof(char) * num);
  }
  void Test(void)
  {
  char *str = NULL;
  GetMemory(str, 100); // str reng ran wei NULL
  strcpy(str, "hello"); // yun xing cuo wu
  }
  shi li 7-4-1 shi tu yong zhi zhen shen shu shen qing dong tai nei cun
  mao bing chu zai han shu GetMemory zhong 。 bian yi qi zong shi yao wei han shu de mei ge shen shu zhi zuo lin shi fu ben , zhi zhen shen shu p de fu ben shi _p, bian yi qi shi _p = p。 jia ru han shu ti nei de cheng xu xiu gai le _p de nei rong , jiu dao zhi shen shu p de nei rong zuo xiang ying de xiu gai 。 zhe jiu shi zhi zhen ke yi yong zuo shu chu shen shu de yuan yin 。 zai ben li zhong ,_p shen qing le xin de nei cun , zhi shi ba _p suo zhi de nei cun di zhi gai bian le , dan shi p si hao wei bian 。 suo yi han shu GetMemory bing bu neng shu chu ren he dong xi 。 shi shi shang , mei zhi xing yi ci GetMemory jiu hui xie lu yi kuai nei cun , yin wei mei you yong free shi fang nei cun 。 geng duo nei rong qing kan C/C++ ji shu zhuan ti Java bian cheng kai fa shou ce zhuan ti , huo
  jia ru fei de yao yong zhi zhen shen shu qu shen qing nei cun , na me ying gai gai yong “ zhi xiang zhi zhen de zhi zhen ”, jian shi li 7-4-2。
  void GetMemory2(char **p, int num)
  {
  *p = (char *)malloc(sizeof(char) * num);
  }
  
   void Test2(void)
  {
  char *str = NULL;
  GetMemory2(&str, 100); // zhu chong shen shu shi &str, er bu shi str
  strcpy(str, "hello");
  cout<< str << endl;
  free(str);
  }
  shi li 7-4-2 yong zhi xiang zhi zhen de zhi zhen shen qing dong tai nei cun
  you yu “ zhi xiang zhi zhen de zhi zhen ” zhe ge gai nian bu qing yi li jie , wo men ke yi yong han shu fan hui zhi lai chuan di dong tai nei cun 。 zhe zhong fang fa geng jia jian dan , jian shi li 7-4-3。
  char *GetMemory3(int num)
  {
  char *p = (char *)malloc(sizeof(char) * num);
  return p;
  }
  void Test3(void)
  {
  char *str = NULL;
  str = GetMemory3(100);
  strcpy(str, "hello");
  cout<< str << endl;
  free(str);
  }
  
   shi li 7-4-3 yong han shu fan hui zhi lai chuan di dong tai nei cun
  yong han shu fan hui zhi lai chuan di dong tai nei cun zhe zhong fang fa sui ran hao yong , dan shi jing chang you ren ba return yu ju yong cuo le 。 zhe li qiang tiao bu yao yong return yu ju fan hui zhi xiang “ zhan nei cun ” de zhi zhen , yin wei gai nei cun zai han shu jie shu shi zi dong xiao wang , jian shi li 7-4-4。
  char *GetString(void)
  {
  char p[] = "hello world";
  return p; // bian yi qi jiang ti chu jing gao
  }
  void Test4(void)
  {
  char *str = NULL;
  str = GetString(); // str de nei rong shi la ji
  cout<< str << endl;
  }
  shi li 7-4-4 return yu ju fan hui zhi xiang “ zhan nei cun ” de zhi zhen
  yong tiao shi qi zhu bu gen zong Test4, fa xian zhi xing str = GetString yu ju hou str bu zai shi NULL zhi zhen , dan shi str de nei rong bu shi “hello world” er shi la ji 。
  jia ru ba shi li 7-4-4 gai xie cheng shi li 7-4-5, hui zen me yang ?
  char *GetString2(void)
  {
  char *p = "hello world";
  return p;
  }
  void Test5(void)
  {
  char *str = NULL;
  str = GetString2();
  cout<< str << endl;
  }
  shi li 7-4-5 return yu ju fan hui chang liang zi fu chuan
  han shu Test5 yun xing sui ran bu hui chu cuo , dan shi han shu GetString2 de she ji gai nian que shi cuo wu de 。 yin wei GetString2 nei de “hello world” shi chang liang zi fu chuan , wei yu jing tai cun chu qu , ta zai cheng xu sheng ming qi nei heng ding bu bian 。 wu lun shen me shi hou tiao yong GetString2, ta fan hui de shi zhong shi tong yi ge “ zhi du ” de nei cun kuai 。
  7.5 free he delete ba zhi zhen zen me la ?
  7.7 du jue “ ye zhi zhen ”
  “ ye zhi zhen ” bu shi NULL zhi zhen , shi zhi xiang “ la ji ” nei cun de zhi zhen 。 ren men yi ban bu hui cuo yong NULL zhi zhen , yin wei yong if yu ju hen qing yi pan ding 。 dan shi “ ye zhi zhen ” shi hen wei xian de ,if yu ju dui ta bu qi zuo yong 。
  “ ye zhi zhen ” de cheng yin zhu yao you liang zhong :
  (1) zhi zhen bian liang mei you bei chu shi hua 。 ren he zhi zhen bian liang gang bei chuang jian shi bu hui zi dong cheng wei NULL zhi zhen , ta de que sheng zhi shi sui ji de , ta hui luan zhi yi qi 。 suo yi , zhi zhen bian liang zai chuang jian de tong shi ying dang bei chu shi hua , yao me jiang zhi zhen she zhi wei NULL, yao me rang ta zhi xiang he fa de nei cun 。 li ru
  char *p = NULL;
  char *str = (char *) malloc(100);
  (2) zhi zhen p bei free huo zhe delete zhi hou , mei you zhi wei NULL, rang ren wu yi wei p shi ge he fa de zhi zhen 。 shen jian 7.5 jie 。
  (3) zhi zhen cao zuo chao yue le bian liang de zuo yong fan wei 。 zhe zhong qing kuang rang ren fang bu sheng fang , shi li cheng xu ru xia :
  class A
  {
  public:
  void Func(void){ cout << “Func of class A” << endl; }
  };
  void Test(void)
  {
  A *p;
  {
  A a;
  p = &a; // zhu chong a de sheng ming qi
  }
  p->Func(); // p shi “ ye zhi zhen ”
  }
  han shu Test zai zhi xing yu ju p->Func() shi , dui xiang a yi jing xiao shi , er p shi zhi xiang a de , suo yi p jiu cheng le “ ye zhi zhen ”。 dan xi qi de shi wo yun xing zhe ge cheng xu shi ju ran mei you chu cuo , zhe ke neng yu bian yi qi you guan 。
  7.8 you le malloc/free wei shen me huan yao new/delete ?
  malloc yu free shi C++/C yu yan de biao zhun ku han shu ,new/delete shi C++ de yun suan fu 。 ta men dou ke yong yu shen qing dong tai nei cun he shi fang nei cun 。
  dui yu fei nei bu shu ju lei xing de dui xiang er yan , guang yong maloc/free wu fa man zu dong tai dui xiang de yao qiu 。 dui xiang zai chuang jian de tong shi yao zi dong zhi xing gou zao han shu , dui xiang zai xiao wang zhi qian yao zi dong zhi xing xi gou han shu 。 you yu malloc/free shi ku han shu er bu shi yun suan fu , bu zai bian yi qi kong zhi quan xian zhi nei , bu neng gou ba zhi xing gou zao han shu he xi gou han shu de ren wu qiang jia yu malloc/free。
  yin ci C++ yu yan xu yao yi ge neng wan cheng dong tai nei cun fen pei he chu shi hua gong zuo de yun suan fu new, yi ji yi ge neng wan cheng qing li yu shi fang nei cun gong zuo de yun suan fu delete。 zhu chong new/delete bu shi ku han shu 。
  wo men xian kan yi kan malloc/free he new/delete ru he shi xian dui xiang de dong tai nei cun zhi li , jian shi li 7-8。
  class Obj
  {
  public :
  Obj(void){ cout << “Initialization” << endl; }
  ~Obj(void){ cout << “Destroy” << endl; }
  void Initialize(void){ cout << “Initialization” << endl; }
  void Destroy(void){ cout << “Destroy” << endl; }
  };
  void UseMallocFree(void)
  {
  Obj *a = (obj *)malloc(sizeof(obj)); // shen qing dong tai nei cun
  a->Initialize(); // chu shi hua
  //…
  a->Destroy(); // qing chu gong zuo
  free(a); // shi fang nei cun
  }
  void UseNewDelete(void)
  {
  Obj *a = new Obj; // shen qing dong tai nei cun bing qie chu shi hua
  //…
  delete a; // qing chu bing qie shi fang nei cun
  } geng duo nei rong qing kan C/C++ ji shu zhuan ti Java bian cheng kai fa shou ce zhuan ti , huo
  
   shi li 7-8 yong malloc/free he new/delete ru he shi xian dui xiang de dong tai nei cun zhi li
  
   lei Obj de han shu Initialize mo ni le gou zao han shu de gong neng , han shu Destroy mo ni le xi gou han shu de gong neng 。 han shu UseMallocFree zhong , you yu malloc/free bu neng zhi xing gou zao han shu yu xi gou han shu , bi xu tiao yong cheng yuan han shu Initialize he Destroy lai wan cheng chu shi hua yu qing chu gong zuo 。 han shu UseNewDelete ze jian dan de duo 。
  suo yi wo men bu yao qi tu yong malloc/free lai wan cheng dong tai dui xiang de nei cun zhi li , ying gai yong new/delete。 you yu nei bu shu ju lei xing de “ dui xiang ” mei you gou zao yu xi gou de guo cheng , dui ta men er yan malloc/free he new/delete shi deng jia de 。
  ji ran new/delete de gong neng wan quan fu gai le malloc/free, wei shen me C++ bu ba malloc/free tao tai chu ju ne ? zhe shi yin wei C++ cheng xu jing chang yao tiao yong C han shu , er C cheng xu zhi neng yong malloc/free zhi li dong tai nei cun 。
  jia ru yong free shi fang “new chuang jian de dong tai dui xiang ”, na me gai dui xiang yin wu fa zhi xing xi gou han shu er ke neng dao zhi cheng xu chu cuo 。 jia ru yong delete shi fang “malloc shen qing de dong tai nei cun ”, li lun shang jiang cheng xu bu hui chu cuo , dan shi gai cheng xu de ke du xing hen cha 。 suo yi new/delete bi xu pei dui shi yong ,malloc/free ye yi yang 。
  7.9 nei cun hao jin zen me ban ?
  jia ru zai shen qing dong tai nei cun shi zhao bu dao zu gou da de nei cun kuai ,malloc he new jiang fan hui NULL zhi zhen , xuan gao nei cun shen qing shi bai 。 tong chang you san zhong fang shi chu li “ nei cun hao jin ” wen ti 。
  (1) pan ding zhi zhen shi fou wei NULL, jia ru shi ze ma shang yong return yu ju zhong zhi ben han shu 。 li ru :
  void Func(void)
  {
  A *a = new A;
  if(a == NULL)
  {
  return;
  }
  …
  }
  (2) pan ding zhi zhen shi fou wei NULL, jia ru shi ze ma shang yong exit(1) zhong zhi zheng ge cheng xu de yun xing 。 li ru :
  void Func(void)
  {
  A *a = new A;
  if(a == NULL)
  {
  cout << “Memory Exhausted” << endl;
  exit(1);
  }
  …
  }
  (3) wei new he malloc she zhi yi chang chu li han shu 。 li ru Visual C++ ke yi yong _set_new_hander han shu wei new she zhi yong hu zi ji ding yi de yi chang chu li han shu , ye ke yi rang malloc xiang yong yu new xiang tong de yi chang chu li han shu 。 ju ti nei rong qing shen kao C++ shi yong shou ce 。
  shang shu (1)(2) fang shi shi yong zui pu bian 。 jia ru yi ge han shu nei you duo chu xu yao shen qing dong tai nei cun , na me fang shi (1) jiu xian de li bu cong xin ( shi fang nei cun hen ma fan ), ying gai yong fang shi (2) lai chu li 。
  hen duo ren bu ren xin yong exit(1), wen :“ bu bian xie chu cuo chu li cheng xu , rang cao zuo xi tong zi ji jie jue xing bu xing ?”
  bu xing 。 jia ru fa sheng “ nei cun hao jin ” zhe yang de shi qing , yi ban shuo lai ying yong cheng xu yi jing wu yao ke jiu 。 jia ru bu yong exit(1) ba huai cheng xu sha si , ta ke neng hui hai si cao zuo xi tong 。 dao li ru tong : jia ru bu ba dai tu ji bi , dai tu zai lao si zhi qian hui fan xia geng duo de zui 。
  you yi ge hen chong yao de xian xiang yao gao su da jia 。 dui yu 32 wei yi shang de ying yong cheng xu er yan , wu lun zen yang shi yong malloc yu new, ji hu bu ke neng dao zhi “ nei cun hao jin ”。 wo zai Windows 98 xia yong Visual C++ bian xie le ce shi cheng xu , jian shi li 7-9。 zhe ge cheng xu hui wu xiu zhi di yun xing xia qu , gen ben bu hui zhong zhi 。 yin wei 32 wei cao zuo xi tong zhi chi “ xu cun ”, nei cun yong wan le , zi dong yong ying pan kong jian ding ti 。 wo zhi ting dao ying pan ga zhi ga zhi di xiang ,Window 98 yi jing lei de dui jian pan 、 shu biao hao wu fan ying 。
  wo ke yi de chu zhe me yi ge jie lun : dui yu 32 wei yi shang de ying yong cheng xu ,“ nei cun hao jin ” cuo wu chu li cheng xu hao wu yong chu 。 zhe xia ke ba Unix he Windows cheng xu yuan men le huai le : fan zheng cuo wu chu li cheng xu bu qi zuo yong , wo jiu bu xie le , sheng le hen duo ma fan 。
  wo bu xiang wu dao du zhe , bi xu qiang tiao : bu jia cuo wu chu li jiang dao zhi cheng xu de zhi liang hen cha , qian wan bu ke yin xiao shi da 。
  void main(void)
  {
  float *p = NULL;
  while(TRUE)
  {
  p = new float[1000000];
  cout << “eat memory” << endl;
  if(p==NULL)
  exit(1);
  }
  }
  shi li 7-9 shi tu hao jin cao zuo xi tong de nei cun
  .10 malloc/free de shi yong yao dian
  han shu malloc de yuan xing ru xia :
  void * malloc(size_t size);
  yong malloc shen qing yi kuai chang du wei length de zheng shu lei xing de nei cun , cheng xu ru xia :
  int *p = (int *) malloc(sizeof(int) * length);
  wo men ying dang ba zhu chong li ji zhong zai liang ge yao su shang :“ lei xing zhuan huan ” he “sizeof”。
  u malloc fan hui zhi de lei xing shi void *, suo yi zai tiao yong malloc shi yao xian shi di jin xing lei xing zhuan huan , jiang void * zhuan huan cheng suo xu yao de zhi zhen lei xing 。
  u malloc han shu ben shen bing bu shi bie yao shen qing de nei cun shi shen me lei xing , ta zhi guan xin nei cun de zong zi jie shu 。 wo men tong chang ji bu zhu int, float deng shu ju lei xing de bian liang de que qie zi jie shu 。 li ru int bian liang zai 16 wei xi tong xia shi 2 ge zi jie , zai 32 wei xia shi 4 ge zi jie ; er float bian liang zai 16 wei xi tong xia shi 4 ge zi jie , zai 32 wei xia ye shi 4 ge zi jie 。 zui hao yong yi xia cheng xu zuo yi ci ce shi :
  cout << sizeof(char) << endl;
  cout << sizeof(int) << endl;
  cout << sizeof(unsigned int) << endl;
  cout << sizeof(long) << endl;
  cout << sizeof(unsigned long) << endl;
  cout << sizeof(float) << endl;
  cout << sizeof(double) << endl;
  cout << sizeof(void *) << endl;
  
   zai malloc de “()” zhong shi yong sizeof yun suan fu shi liang hao de feng ge , dan yao dang xin you shi wo men hui hun le tou , xie chu p = malloc(sizeof(p)) zhe yang de cheng xu lai 。
  u han shu free de yuan xing ru xia :
  void free( void * memblock );
  wei shen me free han shu bu xiang malloc han shu na yang fu duo ne ? zhe shi yin wei zhi zhen p de lei xing yi ji ta suo zhi de nei cun de rong liang shi xian dou shi zhi dao de , yu ju free(p) neng zheng que di shi fang nei cun 。 jia ru p shi NULL zhi zhen , na me free dui p wu lun cao zuo duo shao ci dou bu hui chu wen ti 。 jia ru p bu shi NULL zhi zhen , na me free dui p lian xu cao zuo liang ci jiu hui dao zhi cheng xu yun xing cuo wu 。
  7.11 new/delete de shi yong yao dian
  yun suan fu new shi yong qi lai yao bi han shu malloc jian dan de duo , li ru :
  int *p1 = (int *)malloc(sizeof(int) * length);
  int *p2 = new int[length];
  zhe shi yin wei new nei zhi le sizeof、 lei xing zhuan huan he lei xing an quan jian cha gong neng 。 dui yu fei nei bu shu ju lei xing de dui xiang er yan ,new zai chuang jian dong tai dui xiang de tong shi wan cheng le chu shi hua gong zuo 。 jia ru dui xiang you duo ge gou zao han shu , na me new de yu ju ye ke yi you duo zhong xing shi 。 li ru
  class Obj
  {
  public :
  Obj(void); // wu shen shu de gou zao han shu
  Obj(int x); // dai yi ge shen shu de gou zao han shu
  …
  }
  void Test(void)
  {
  Obj *a = new Obj;
  Obj *b = new Obj(1); // chu zhi wei 1
  …
  delete a;
  delete b;
  }
  jia ru yong new chuang jian dui xiang shu zu , na me zhi neng shi yong dui xiang de wu shen shu gou zao han shu 。 li ru
  Obj *objects = new Obj[100]; // chuang jian 100 ge dong tai dui xiang
  bu neng xie cheng
  Obj *objects = new Obj[100](1);// chuang jian 100 ge dong tai dui xiang de tong shi fu chu zhi 1
  zai yong delete shi fang dui xiang shu zu shi , liu yi bu yao diu le fu hao ‘[]’。 li ru
  delete []objects; // zheng que de yong fa
  delete objects; // cuo wu de yong fa
  hou zhe xiang dang yu delete objects[0], lou diao le ling wai 99 ge dui xiang 。
  7.12 yi xie xin de ti hui
  wo shu xi bu shao ji shu bu cuo de C++/C cheng xu yuan , hen shao you ren neng pai pai xiong pu shuo tong xiao zhi zhen yu nei cun zhi li ( bao kuo wo zi ji )。 wo zui chu xue xi C yu yan shi fei fan pa zhi zhen , dao zhi wo kai fa di yi ge ying yong ruan jian ( yue 1 wan xing C dai ma ) shi mei you shi yong yi ge zhi zhen , quan yong shu zu lai ding ti zhi zhen , shi zai chun ben de guo fen 。 duo bi zhi zhen bu shi ban fa , hou lai wo gai xie le zhe ge ruan jian , dai ma liang suo xiao dao yuan xian de yi ban 。
  wo de jing yan jiao xun shi :
  (1) yue shi pa zhi zhen , jiu yue yao shi yong zhi zhen 。 bu hui zheng que shi yong zhi zhen , ken ding suan bu shang shi he ge de cheng xu yuan 。
  (2) bi xu yang cheng “ shi yong tiao shi qi zhu bu gen zong cheng xu ” de xi guan , zhi you zhe yang cai neng fa xian wen ti de ben zhi 。 geng duo nei rong qing kan C/C++ ji shu zhuan ti Java bian cheng kai fa shou ce zhuan ti , huo【原文】【汉音对照
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

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

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

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

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

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

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

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

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

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

 
 
di 7 zhang nei cun zhi li    huan ying jin ru nei cun zhe pian lei qu 。 wei da de Bill Gates zeng jing shi yan :   640K ought to be enough for everybody   — Bill Gates 1981 cheng xu yuan men jing chang bian xie nei cun zhi li cheng xu , wang wang ti xin diao dan 。 jia ru bu xiang chu lei , wei yi de jie jue ban fa jiu shi fa xian suo you qian fu de di lei bing qie pai chu ta men , duo shi duo bu le de 。 ben zhang de nei rong bi yi ban jiao ke shu de yao shen ru de duo , du zhe xu xi xin yue du , zuo dao zhen zheng di tong xiao nei cun zhi li 。   7.1 nei cun fen pei fang shi    nei cun fen pei fang shi you san zhong :   (1) cong jing tai cun chu qu yu fen pei 。 nei cun zai cheng xu bian yi de shi hou jiu yi jing fen pei hao , zhe kuai nei cun zai cheng xu de zheng ge yun xing qi jian dou cun zai 。 li ru quan ju bian liang ,static bian liang 。   (2) zai zhan shang chuang jian 。 zai zhi xing han shu shi , han shu nei ju bu bian liang de cun chu dan yuan dou ke yi zai zhan shang chuang jian , han shu zhi xing jie shu shi zhe xie cun chu dan yuan zi dong bei shi fang 。 zhan nei cun fen pei yun suan nei zhi yu chu li qi de zhi ling ji zhong , xiao lv hen gao , dan shi fen pei de nei cun rong liang you xian 。   (3) cong dui shang fen pei , yi cheng dong tai nei cun fen pei 。 cheng xu zai yun xing de shi hou yong malloc huo new shen qing ren yi duo shao de nei cun , cheng xu yuan zi ji fu ze zai he shi yong free huo delete shi fang nei cun 。 dong tai nei cun de sheng cun qi you wo men jue ding , shi yong fei chang ling huo , dan wen ti ye zui duo 。   7.2 chang jian de nei cun cuo wu ji qi dui ce    fa sheng nei cun cuo wu shi jian fei chang ma fan de shi qing 。 bian yi qi bu neng zi dong fa xian zhe xie cuo wu , tong chang shi zai cheng xu yun xing shi cai neng bu zhuo dao 。 er zhe xie cuo wu da duo mei you ming xian de zheng zhuang , shi yin shi xian , zeng jia le gai cuo de nan du 。 you shi yong hu nu qi chong chong di ba ni zhao lai , cheng xu que mei you fa sheng ren he wen ti , ni yi zou , cuo wu you fa zuo le 。    chang jian de nei cun cuo wu ji qi dui ce ru xia :   u nei cun fen pei wei cheng gong , que shi yong le ta 。    bian cheng xin shou chang fan zhe zhong cuo wu , yin wei ta men mei you yi shi dao nei cun fen pei hui bu cheng gong 。 chang yong jie jue ban fa shi , zai shi yong nei cun zhi qian jian cha zhi zhen shi fou wei NULL。 jia ru zhi zhen p shi han shu de shen shu , na me zai han shu de ru kou chu yong assert(p!=NULL) jin xing jian cha 。 jia ru shi yong malloc huo new lai shen qing nei cun , ying gai yong if(p==NULL) huo if(p!=NULL) jin xing fang cuo chu li 。   u nei cun fen pei sui ran cheng gong , dan shi shang wei chu shi hua jiu yin yong ta 。    fan zhe zhong cuo wu zhu yao you liang ge qi yin : yi shi mei you chu shi hua de guan nian ; er shi wu yi wei nei cun de que sheng chu zhi quan wei ling , dao zhi yin yong chu zhi cuo wu ( li ru shu zu )。    nei cun de que sheng chu zhi jiu jing shi shen me bing mei you tong yi de biao zhun , jin guan you xie shi hou wei ling zhi , wo men ning ke xin qi wu bu ke xin qi you 。 suo yi wu lun yong he zhong fang shi chuang jian shu zu , dou bie wang le fu chu zhi , ji bian shi fu ling zhi ye bu ke sheng lue , bu yao xian ma fan 。   u nei cun fen pei cheng gong bing qie yi jing chu shi hua , dan cao zuo yue guo le nei cun de bian jie 。    li ru zai shi yong shu zu shi jing chang fa sheng xia biao “ duo 1” huo zhe “ shao 1” de cao zuo 。 fei fan shi zai for xun huan yu ju zhong , xun huan ci shu hen qing yi gao cuo , dao zhi shu zu cao zuo yue jie 。   u wang ji le shi fang nei cun , zao cheng nei cun xie lu 。    han you zhe zhong cuo wu de han shu mei bei tiao yong yi ci jiu diu shi yi kuai nei cun 。 gang kai shi shi xi tong de nei cun chong zu , ni kan bu dao cuo wu 。 zhong you yi ci cheng xu hu ran si diao , xi tong chu xian ti shi : nei cun hao jin 。    dong tai nei cun de shen qing yu shi fang bi xu pei dui , cheng xu zhong malloc yu free de shi yong ci shu yi ding yao xiang tong , fou ze ken ding you cuo wu (new/delete tong li )。   u shi fang le nei cun que ji xu shi yong ta 。    you san zhong qing kuang :   (1) cheng xu zhong de dui xiang tiao yong guan xi guo yu fu duo , shi zai nan yi gao qing chu mou ge dui xiang jiu jing shi fou yi jing shi fang le nei cun , ci shi ying gai chong xin she ji shu ju jie gou , cong gen ben shang jie jue dui xiang zhi li de hun luan ju mian 。   (2) han shu de return yu ju xie cuo le , zhu chong bu yao fan hui zhi xiang “ zhan nei cun ” de “ zhi zhen ” huo zhe “ yin yong ”, yin wei gai nei cun zai han shu ti jie shu shi bei zi dong xiao hui 。   (3) shi yong free huo delete shi fang le nei cun hou , mei you jiang zhi zhen she zhi wei NULL。 dao zhi chan sheng “ ye zhi zhen ”。   l 【 gui ze 7-2-1】 yong malloc huo new shen qing nei cun zhi hou , ying gai li ji jian cha zhi zhen zhi shi fou wei NULL。 fang zhi shi yong zhi zhen zhi wei NULL de nei cun 。   l 【 gui ze 7-2-2】 bu yao wang ji wei shu zu he dong tai nei cun fu chu zhi 。 fang zhi jiang wei bei chu shi hua de nei cun zuo wei you zhi shi yong 。   l 【 gui ze 7-2-3】 bi mian shu zu huo zhi zhen de xia biao yue jie , fei fan yao dang xin fa sheng “ duo 1” huo zhe “ shao 1” cao zuo 。   l 【 gui ze 7-2-4】 dong tai nei cun de shen qing yu shi fang bi xu pei dui , fang zhi nei cun xie lou 。   l 【 gui ze 7-2-5】 yong free huo delete shi fang le nei cun zhi hou , li ji jiang zhi zhen she zhi wei NULL, fang zhi chan sheng “ ye zhi zhen ”。   7.3 zhi zhen yu shu zu de dui bi   C++/C cheng xu zhong , zhi zhen he shu zu zai bu shao di fang ke yi xiang hu ti huan zhe yong , rang ren chan sheng yi zhong cuo jiao , yi wei liang zhe shi deng jia de 。    shu zu yao me zai jing tai cun chu qu bei chuang jian ( ru quan ju shu zu ), yao me zai zhan shang bei chuang jian 。 shu zu ming dui ying zhe ( er bu shi zhi xiang ) yi kuai nei cun , qi di zhi yu rong liang zai sheng ming qi nei bao chi bu bian , zhi you shu zu de nei rong ke yi gai bian 。    zhi zhen ke yi sui shi zhi xiang ren yi lei xing de nei cun kuai , ta de te zheng shi “ ke bian ”, suo yi wo men chang yong zhi zhen lai cao zuo dong tai nei cun 。 zhi zhen yuan bi shu zu ling huo , dan ye geng wei xian 。    xia mian yi zi fu chuan wei li bi jiao zhi zhen yu shu zu de te xing 。   7.3.1 xiu gai nei rong    shi li 7-3-1 zhong , zi fu shu zu a de rong liang shi 6 ge zi fu , qi nei rong wei hello\0。a de nei rong ke yi gai bian , ru a[0]= ‘X’。 zhi zhen p zhi xiang chang liang zi fu chuan “world”( wei yu jing tai cun chu qu , nei rong wei world\0), chang liang zi fu chuan de nei rong shi bu ke yi bei xiu gai de 。 cong yu fa shang kan , bian yi qi bing bu jiao de yu ju p[0]= ‘X’ you shen me bu tuo , dan shi gai yu ju qi tu xiu gai chang liang zi fu chuan de nei rong er dao zhi yun xing cuo wu 。   char a[] = “hello”;   a[0] = ‘X’;   cout << a << endl;   char *p = “world”; // zhu chong p zhi xiang chang liang zi fu chuan   p[0] = ‘X’; // bian yi qi bu neng fa xian gai cuo wu   cout << p << endl;    shi li 7-3-1 xiu gai shu zu he zhi zhen de nei rong   7.3.2 nei rong fu zhi yu bi jiao    bu neng dui shu zu ming jin xing zhi jie fu zhi yu bi jiao 。 shi li 7-3-2 zhong , ruo xiang ba shu zu a de nei rong fu zhi gei shu zu b, bu neng yong yu ju b = a , fou ze jiang chan sheng bian yi cuo wu 。 ying gai yong biao zhun ku han shu strcpy jin xing fu zhi 。 tong li , bi jiao b he a de nei rong shi fou xiang tong , bu neng yong if(b==a) lai pan ding , ying gai yong biao zhun ku han shu strcmp jin xing bi jiao 。    yu ju p = a bing bu neng ba a de nei rong fu zhi zhi zhen p, er shi ba a de di zhi fu gei le p。 yao xiang fu zhi a de nei rong , ke yi xian yong ku han shu malloc wei p shen qing yi kuai rong liang wei strlen(a)+1 ge zi fu de nei cun , zai yong strcpy jin xing zi fu chuan fu zhi 。 tong li , yu ju if(p==a) bi jiao de bu shi nei rong er shi di zhi , ying gai yong ku han shu strcmp lai bi jiao 。   // shu zu …   char a[] = "hello";   char b[10];   strcpy(b, a); // bu neng yong b = a;   if(strcmp(b, a) == 0) // bu neng yong if (b == a)   …   // zhi zhen …   int len = strlen(a);   char *p = (char *)malloc(sizeof(char)*(len+1));   strcpy(p,a); // bu yao yong p = a;   if(strcmp(p, a) == 0) // bu yao yong if (p == a)   …    shi li 7-3-2 shu zu he zhi zhen de nei rong fu zhi yu bi jiao   7.3.3 ji suan nei cun rong liang    yong yun suan fu sizeof ke yi ji suan chu shu zu de rong liang ( zi jie shu )。 shi li 7-3-3(a) zhong ,sizeof(a) de zhi shi 12( zhu chong bie wang le ’\0’)。 zhi zhen p zhi xiang a, dan shi sizeof(p) de zhi que shi 4。 zhe shi yin wei sizeof(p) de dao de shi yi ge zhi zhen bian liang de zi jie shu , xiang dang yu sizeof(char*), er bu shi p suo zhi de nei cun rong liang 。C++/C yu yan mei you ban fa zhi dao zhi zhen suo zhi de nei cun rong liang , chu fei zai shen qing nei cun shi ji zhu ta 。    zhu chong dang shu zu zuo wei han shu de shen shu jin xing chuan di shi , gai shu zu zi dong tui hua wei tong lei xing de zhi zhen 。 shi li 7-3-3(b) zhong , bu lun shu zu a de rong liang shi duo shao ,sizeof(a) shi zhong deng yu sizeof(char *)。   char a[] = "hello world";   char *p = a;   cout<< sizeof(a) << endl; // 12 zi jie   cout<< sizeof(p) << endl; // 4 zi jie    shi li 7-3-3(a) ji suan shu zu he zhi zhen de nei cun rong liang   void Func(char a[100])   {   cout<< sizeof(a) << endl; // 4 zi jie er bu shi 100 zi jie   }    shi li 7-3-3(b) shu zu tui hua wei zhi zhen   7.4 zhi zhen shen shu shi ru he chuan di nei cun de ?    jia ru han shu de shen shu shi yi ge zhi zhen , bu yao zhi wang yong gai zhi zhen qu shen qing dong tai nei cun 。 shi li 7-4-1 zhong ,Test han shu de yu ju GetMemory(str, 200) bing mei you shi str huo de qi wang de nei cun ,str yi jiu shi NULL, wei shen me ?   void GetMemory(char *p, int num)   {   p = (char *)malloc(sizeof(char) * num);   }   void Test(void)   {   char *str = NULL;   GetMemory(str, 100); // str reng ran wei NULL   strcpy(str, "hello"); // yun xing cuo wu   }    shi li 7-4-1 shi tu yong zhi zhen shen shu shen qing dong tai nei cun    mao bing chu zai han shu GetMemory zhong 。 bian yi qi zong shi yao wei han shu de mei ge shen shu zhi zuo lin shi fu ben , zhi zhen shen shu p de fu ben shi _p, bian yi qi shi _p = p。 jia ru han shu ti nei de cheng xu xiu gai le _p de nei rong , jiu dao zhi shen shu p de nei rong zuo xiang ying de xiu gai 。 zhe jiu shi zhi zhen ke yi yong zuo shu chu shen shu de yuan yin 。 zai ben li zhong ,_p shen qing le xin de nei cun , zhi shi ba _p suo zhi de nei cun di zhi gai bian le , dan shi p si hao wei bian 。 suo yi han shu GetMemory bing bu neng shu chu ren he dong xi 。 shi shi shang , mei zhi xing yi ci GetMemory jiu hui xie lu yi kuai nei cun , yin wei mei you yong free shi fang nei cun 。 geng duo nei rong qing kan C/C++ ji shu zhuan ti Java bian cheng kai fa shou ce zhuan ti , huo    jia ru fei de yao yong zhi zhen shen shu qu shen qing nei cun , na me ying gai gai yong “ zhi xiang zhi zhen de zhi zhen ”, jian shi li 7-4-2。   void GetMemory2(char **p, int num)   {   *p = (char *)malloc(sizeof(char) * num);   }   void Test2(void)   {   char *str = NULL;   GetMemory2(&str, 100); // zhu chong shen shu shi &str, er bu shi str   strcpy(str, "hello");   cout<< str << endl;   free(str);   }    shi li 7-4-2 yong zhi xiang zhi zhen de zhi zhen shen qing dong tai nei cun    you yu “ zhi xiang zhi zhen de zhi zhen ” zhe ge gai nian bu qing yi li jie , wo men ke yi yong han shu fan hui zhi lai chuan di dong tai nei cun 。 zhe zhong fang fa geng jia jian dan , jian shi li 7-4-3。   char *GetMemory3(int num)   {   char *p = (char *)malloc(sizeof(char) * num);   return p;   }   void Test3(void)   {   char *str = NULL;   str = GetMemory3(100);   strcpy(str, "hello");   cout<< str << endl;   free(str);   }    shi li 7-4-3 yong han shu fan hui zhi lai chuan di dong tai nei cun    yong han shu fan hui zhi lai chuan di dong tai nei cun zhe zhong fang fa sui ran hao yong , dan shi jing chang you ren ba return yu ju yong cuo le 。 zhe li qiang tiao bu yao yong return yu ju fan hui zhi xiang “ zhan nei cun ” de zhi zhen , yin wei gai nei cun zai han shu jie shu shi zi dong xiao wang , jian shi li 7-4-4。   char *GetString(void)   {   char p[] = "hello world";   return p; // bian yi qi jiang ti chu jing gao   }   void Test4(void)   {   char *str = NULL;   str = GetString(); // str de nei rong shi la ji   cout<< str << endl;   }    shi li 7-4-4 return yu ju fan hui zhi xiang “ zhan nei cun ” de zhi zhen    yong tiao shi qi zhu bu gen zong Test4, fa xian zhi xing str = GetString yu ju hou str bu zai shi NULL zhi zhen , dan shi str de nei rong bu shi “hello world” er shi la ji 。    jia ru ba shi li 7-4-4 gai xie cheng shi li 7-4-5, hui zen me yang ?   char *GetString2(void)   {   char *p = "hello world";   return p;   }   void Test5(void)   {   char *str = NULL;   str = GetString2();   cout<< str << endl;   }    shi li 7-4-5 return yu ju fan hui chang liang zi fu chuan    han shu Test5 yun xing sui ran bu hui chu cuo , dan shi han shu GetString2 de she ji gai nian que shi cuo wu de 。 yin wei GetString2 nei de “hello world” shi chang liang zi fu chuan , wei yu jing tai cun chu qu , ta zai cheng xu sheng ming qi nei heng ding bu bian 。 wu lun shen me shi hou tiao yong GetString2, ta fan hui de shi zhong shi tong yi ge “ zhi du ” de nei cun kuai 。   7.5 free he delete ba zhi zhen zen me la ?   7.7 du jue “ ye zhi zhen ”   “ ye zhi zhen ” bu shi NULL zhi zhen , shi zhi xiang “ la ji ” nei cun de zhi zhen 。 ren men yi ban bu hui cuo yong NULL zhi zhen , yin wei yong if yu ju hen qing yi pan ding 。 dan shi “ ye zhi zhen ” shi hen wei xian de ,if yu ju dui ta bu qi zuo yong 。   “ ye zhi zhen ” de cheng yin zhu yao you liang zhong :   (1) zhi zhen bian liang mei you bei chu shi hua 。 ren he zhi zhen bian liang gang bei chuang jian shi bu hui zi dong cheng wei NULL zhi zhen , ta de que sheng zhi shi sui ji de , ta hui luan zhi yi qi 。 suo yi , zhi zhen bian liang zai chuang jian de tong shi ying dang bei chu shi hua , yao me jiang zhi zhen she zhi wei NULL, yao me rang ta zhi xiang he fa de nei cun 。 li ru   char *p = NULL;   char *str = (char *) malloc(100);   (2) zhi zhen p bei free huo zhe delete zhi hou , mei you zhi wei NULL, rang ren wu yi wei p shi ge he fa de zhi zhen 。 shen jian 7.5 jie 。   (3) zhi zhen cao zuo chao yue le bian liang de zuo yong fan wei 。 zhe zhong qing kuang rang ren fang bu sheng fang , shi li cheng xu ru xia :   class A   {   public:   void Func(void){ cout << “Func of class A” << endl; }   };   void Test(void)   {   A *p;   {   A a;   p = &a; // zhu chong a de sheng ming qi   }   p->Func(); // p shi “ ye zhi zhen ”   }    han shu Test zai zhi xing yu ju p->Func() shi , dui xiang a yi jing xiao shi , er p shi zhi xiang a de , suo yi p jiu cheng le “ ye zhi zhen ”。 dan xi qi de shi wo yun xing zhe ge cheng xu shi ju ran mei you chu cuo , zhe ke neng yu bian yi qi you guan 。   7.8 you le malloc/free wei shen me huan yao new/delete ?   malloc yu free shi C++/C yu yan de biao zhun ku han shu ,new/delete shi C++ de yun suan fu 。 ta men dou ke yong yu shen qing dong tai nei cun he shi fang nei cun 。    dui yu fei nei bu shu ju lei xing de dui xiang er yan , guang yong maloc/free wu fa man zu dong tai dui xiang de yao qiu 。 dui xiang zai chuang jian de tong shi yao zi dong zhi xing gou zao han shu , dui xiang zai xiao wang zhi qian yao zi dong zhi xing xi gou han shu 。 you yu malloc/free shi ku han shu er bu shi yun suan fu , bu zai bian yi qi kong zhi quan xian zhi nei , bu neng gou ba zhi xing gou zao han shu he xi gou han shu de ren wu qiang jia yu malloc/free。    yin ci C++ yu yan xu yao yi ge neng wan cheng dong tai nei cun fen pei he chu shi hua gong zuo de yun suan fu new, yi ji yi ge neng wan cheng qing li yu shi fang nei cun gong zuo de yun suan fu delete。 zhu chong new/delete bu shi ku han shu 。    wo men xian kan yi kan malloc/free he new/delete ru he shi xian dui xiang de dong tai nei cun zhi li , jian shi li 7-8。   class Obj   {   public :   Obj(void){ cout << “Initialization” << endl; }   ~Obj(void){ cout << “Destroy” << endl; }   void Initialize(void){ cout << “Initialization” << endl; }   void Destroy(void){ cout << “Destroy” << endl; }   };   void UseMallocFree(void)   {   Obj *a = (obj *)malloc(sizeof(obj)); // shen qing dong tai nei cun   a->Initialize(); // chu shi hua   //…   a->Destroy(); // qing chu gong zuo   free(a); // shi fang nei cun   }   void UseNewDelete(void)   {   Obj *a = new Obj; // shen qing dong tai nei cun bing qie chu shi hua   //…   delete a; // qing chu bing qie shi fang nei cun   } geng duo nei rong qing kan C/C++ ji shu zhuan ti Java bian cheng kai fa shou ce zhuan ti , huo    shi li 7-8 yong malloc/free he new/delete ru he shi xian dui xiang de dong tai nei cun zhi li    lei Obj de han shu Initialize mo ni le gou zao han shu de gong neng , han shu Destroy mo ni le xi gou han shu de gong neng 。 han shu UseMallocFree zhong , you yu malloc/free bu neng zhi xing gou zao han shu yu xi gou han shu , bi xu tiao yong cheng yuan han shu Initialize he Destroy lai wan cheng chu shi hua yu qing chu gong zuo 。 han shu UseNewDelete ze jian dan de duo 。    suo yi wo men bu yao qi tu yong malloc/free lai wan cheng dong tai dui xiang de nei cun zhi li , ying gai yong new/delete。 you yu nei bu shu ju lei xing de “ dui xiang ” mei you gou zao yu xi gou de guo cheng , dui ta men er yan malloc/free he new/delete shi deng jia de 。    ji ran new/delete de gong neng wan quan fu gai le malloc/free, wei shen me C++ bu ba malloc/free tao tai chu ju ne ? zhe shi yin wei C++ cheng xu jing chang yao tiao yong C han shu , er C cheng xu zhi neng yong malloc/free zhi li dong tai nei cun 。    jia ru yong free shi fang “new chuang jian de dong tai dui xiang ”, na me gai dui xiang yin wu fa zhi xing xi gou han shu er ke neng dao zhi cheng xu chu cuo 。 jia ru yong delete shi fang “malloc shen qing de dong tai nei cun ”, li lun shang jiang cheng xu bu hui chu cuo , dan shi gai cheng xu de ke du xing hen cha 。 suo yi new/delete bi xu pei dui shi yong ,malloc/free ye yi yang 。      7.9 nei cun hao jin zen me ban ?    jia ru zai shen qing dong tai nei cun shi zhao bu dao zu gou da de nei cun kuai ,malloc he new jiang fan hui NULL zhi zhen , xuan gao nei cun shen qing shi bai 。 tong chang you san zhong fang shi chu li “ nei cun hao jin ” wen ti 。   (1) pan ding zhi zhen shi fou wei NULL, jia ru shi ze ma shang yong return yu ju zhong zhi ben han shu 。 li ru :   void Func(void)   {   A *a = new A;   if(a == NULL)   {   return;   }   …   }   (2) pan ding zhi zhen shi fou wei NULL, jia ru shi ze ma shang yong exit(1) zhong zhi zheng ge cheng xu de yun xing 。 li ru :   void Func(void)   {   A *a = new A;   if(a == NULL)   {   cout << “Memory Exhausted” << endl;   exit(1);   }   …   }   (3) wei new he malloc she zhi yi chang chu li han shu 。 li ru Visual C++ ke yi yong _set_new_hander han shu wei new she zhi yong hu zi ji ding yi de yi chang chu li han shu , ye ke yi rang malloc xiang yong yu new xiang tong de yi chang chu li han shu 。 ju ti nei rong qing shen kao C++ shi yong shou ce 。    shang shu (1)(2) fang shi shi yong zui pu bian 。 jia ru yi ge han shu nei you duo chu xu yao shen qing dong tai nei cun , na me fang shi (1) jiu xian de li bu cong xin ( shi fang nei cun hen ma fan ), ying gai yong fang shi (2) lai chu li 。    hen duo ren bu ren xin yong exit(1), wen :“ bu bian xie chu cuo chu li cheng xu , rang cao zuo xi tong zi ji jie jue xing bu xing ?”    bu xing 。 jia ru fa sheng “ nei cun hao jin ” zhe yang de shi qing , yi ban shuo lai ying yong cheng xu yi jing wu yao ke jiu 。 jia ru bu yong exit(1) ba huai cheng xu sha si , ta ke neng hui hai si cao zuo xi tong 。 dao li ru tong : jia ru bu ba dai tu ji bi , dai tu zai lao si zhi qian hui fan xia geng duo de zui 。    you yi ge hen chong yao de xian xiang yao gao su da jia 。 dui yu 32 wei yi shang de ying yong cheng xu er yan , wu lun zen yang shi yong malloc yu new, ji hu bu ke neng dao zhi “ nei cun hao jin ”。 wo zai Windows 98 xia yong Visual C++ bian xie le ce shi cheng xu , jian shi li 7-9。 zhe ge cheng xu hui wu xiu zhi di yun xing xia qu , gen ben bu hui zhong zhi 。 yin wei 32 wei cao zuo xi tong zhi chi “ xu cun ”, nei cun yong wan le , zi dong yong ying pan kong jian ding ti 。 wo zhi ting dao ying pan ga zhi ga zhi di xiang ,Window 98 yi jing lei de dui jian pan 、 shu biao hao wu fan ying 。    wo ke yi de chu zhe me yi ge jie lun : dui yu 32 wei yi shang de ying yong cheng xu ,“ nei cun hao jin ” cuo wu chu li cheng xu hao wu yong chu 。 zhe xia ke ba Unix he Windows cheng xu yuan men le huai le : fan zheng cuo wu chu li cheng xu bu qi zuo yong , wo jiu bu xie le , sheng le hen duo ma fan 。    wo bu xiang wu dao du zhe , bi xu qiang tiao : bu jia cuo wu chu li jiang dao zhi cheng xu de zhi liang hen cha , qian wan bu ke yin xiao shi da 。   void main(void)   {   float *p = NULL;   while(TRUE)   {   p = new float[1000000];   cout << “eat memory” << endl;   if(p==NULL)   exit(1);   }   }    shi li 7-9 shi tu hao jin cao zuo xi tong de nei cun    .10 malloc/free de shi yong yao dian    han shu malloc de yuan xing ru xia :   void * malloc(size_t size);    yong malloc shen qing yi kuai chang du wei length de zheng shu lei xing de nei cun , cheng xu ru xia :   int *p = (int *) malloc(sizeof(int) * length);    wo men ying dang ba zhu chong li ji zhong zai liang ge yao su shang :“ lei xing zhuan huan ” he “sizeof”。   u malloc fan hui zhi de lei xing shi void *, suo yi zai tiao yong malloc shi yao xian shi di jin xing lei xing zhuan huan , jiang void * zhuan huan cheng suo xu yao de zhi zhen lei xing 。   u malloc han shu ben shen bing bu shi bie yao shen qing de nei cun shi shen me lei xing , ta zhi guan xin nei cun de zong zi jie shu 。 wo men tong chang ji bu zhu int, float deng shu ju lei xing de bian liang de que qie zi jie shu 。 li ru int bian liang zai 16 wei xi tong xia shi 2 ge zi jie , zai 32 wei xia shi 4 ge zi jie ; er float bian liang zai 16 wei xi tong xia shi 4 ge zi jie , zai 32 wei xia ye shi 4 ge zi jie 。 zui hao yong yi xia cheng xu zuo yi ci ce shi :   cout << sizeof(char) << endl;   cout << sizeof(int) << endl;   cout << sizeof(unsigned int) << endl;   cout << sizeof(long) << endl;   cout << sizeof(unsigned long) << endl;   cout << sizeof(float) << endl;   cout << sizeof(double) << endl;   cout << sizeof(void *) << endl;    zai malloc de “()” zhong shi yong sizeof yun suan fu shi liang hao de feng ge , dan yao dang xin you shi wo men hui hun le tou , xie chu p = malloc(sizeof(p)) zhe yang de cheng xu lai 。   u han shu free de yuan xing ru xia :   void free( void * memblock );    wei shen me free han shu bu xiang malloc han shu na yang fu duo ne ? zhe shi yin wei zhi zhen p de lei xing yi ji ta suo zhi de nei cun de rong liang shi xian dou shi zhi dao de , yu ju free(p) neng zheng que di shi fang nei cun 。 jia ru p shi NULL zhi zhen , na me free dui p wu lun cao zuo duo shao ci dou bu hui chu wen ti 。 jia ru p bu shi NULL zhi zhen , na me free dui p lian xu cao zuo liang ci jiu hui dao zhi cheng xu yun xing cuo wu 。   7.11 new/delete de shi yong yao dian    yun suan fu new shi yong qi lai yao bi han shu malloc jian dan de duo , li ru :   int *p1 = (int *)malloc(sizeof(int) * length);   int *p2 = new int[length];    zhe shi yin wei new nei zhi le sizeof、 lei xing zhuan huan he lei xing an quan jian cha gong neng 。 dui yu fei nei bu shu ju lei xing de dui xiang er yan ,new zai chuang jian dong tai dui xiang de tong shi wan cheng le chu shi hua gong zuo 。 jia ru dui xiang you duo ge gou zao han shu , na me new de yu ju ye ke yi you duo zhong xing shi 。 li ru   class Obj   {   public :   Obj(void); // wu shen shu de gou zao han shu   Obj(int x); // dai yi ge shen shu de gou zao han shu   …   }   void Test(void)   {   Obj *a = new Obj;   Obj *b = new Obj(1); // chu zhi wei 1   …   delete a;   delete b;   }    jia ru yong new chuang jian dui xiang shu zu , na me zhi neng shi yong dui xiang de wu shen shu gou zao han shu 。 li ru   Obj *objects = new Obj[100]; // chuang jian 100 ge dong tai dui xiang    bu neng xie cheng   Obj *objects = new Obj[100](1);// chuang jian 100 ge dong tai dui xiang de tong shi fu chu zhi 1    zai yong delete shi fang dui xiang shu zu shi , liu yi bu yao diu le fu hao ‘[]’。 li ru   delete []objects; // zheng que de yong fa   delete objects; // cuo wu de yong fa    hou zhe xiang dang yu delete objects[0], lou diao le ling wai 99 ge dui xiang 。   7.12 yi xie xin de ti hui    wo shu xi bu shao ji shu bu cuo de C++/C cheng xu yuan , hen shao you ren neng pai pai xiong pu shuo tong xiao zhi zhen yu nei cun zhi li ( bao kuo wo zi ji )。 wo zui chu xue xi C yu yan shi fei fan pa zhi zhen , dao zhi wo kai fa di yi ge ying yong ruan jian ( yue 1 wan xing C dai ma ) shi mei you shi yong yi ge zhi zhen , quan yong shu zu lai ding ti zhi zhen , shi zai chun ben de guo fen 。 duo bi zhi zhen bu shi ban fa , hou lai wo gai xie le zhe ge ruan jian , dai ma liang suo xiao dao yuan xian de yi ban 。    wo de jing yan jiao xun shi :   (1) yue shi pa zhi zhen , jiu yue yao shi yong zhi zhen 。 bu hui zheng que shi yong zhi zhen , ken ding suan bu shang shi he ge de cheng xu yuan 。   (2) bi xu yang cheng “ shi yong tiao shi qi zhu bu gen zong cheng xu ” de xi guan , zhi you zhe yang cai neng fa xian wen ti de ben zhi 。 geng duo nei rong qing kan C/C++ ji shu zhuan ti Java bian cheng kai fa shou ce zhuan ti , huo
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
我们约会吧
漂亮白皙的模特
好纯好美的女生
靓丽的网络美女
痞子的甘南日记
疑是银河落九天
雪域坝上四——纯美色
冬日恋歌——西城杨柳弄轻柔
 
>>返回首页<<
 为你推荐
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有