用 new 创建的对象用 delete 回收,用 new[] 创建的对象用 delete[] 回收。
基本类型的对象没有析构函数,所以基本类型组成的数字空间都可以用 delete 或者 delete[] 回收;
对于类对象数组,只能用delete[];
对于 new 的单个对象,只能用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;}当使用 delete 去回收类数组空间时,只回收了c[0]。这就是问题所在。
新闻热点
疑难解答
图片精选