用 new 創(chuàng)建的對象用 delete 回收,用 new[] 創(chuàng)建的對象用 delete[] 回收。
基本類型的對象沒有析構(gòu)函數(shù),所以基本類型組成的數(shù)字空間都可以用 delete 或者 delete[] 回收;
對于類對象數(shù)組,只能用delete[];
對于 new 的單個(gè)對象,只能用delete。
class A{public: A(){ cout<<"constructor"<<endl; } ~A(){ cout<<"destructor"<<endl; }};int mian( void ){ int num = 5; A * c1 = new A[ num ]; cout<<hex<<c1<<endl; delete c1; A * c2 = new A[ num ]; cout<<hex<<c2<<endl; delete[] c2; return 0;}當(dāng)使用 delete 去回收類數(shù)組空間時(shí),只回收了c[0]。這就是問題所在。
新聞熱點(diǎn)
疑難解答
圖片精選