2、运算符 delete 形式 delete指针名;∥释放非数组内存单元 deletel]指针名;/释放数组内存单元 例子: int*pl=new int(1) Date * p2=new date[ 5]; Delete pI Deletel p2
2、运算符delete 形式: delete 指针名; //释放非数组内存单元 delete[] 指针名;//释放数组内存单元 例子: int *p1 =new int(1); Date *p2 = new date[5]; Delete p1; Delete[] p2;
Int *p=new int p p-new int ∥存泄漏
Int *p=new int; *p=2; p=new int; //内存泄漏
类中的数据成员也可以是指向堆中某一内存单元的指针,内存 单元可以由构造函数分配,析构函数来释放 l/exp4 9 #include <iostream. h> class a A(int xl=0, int y1=0); Ao void setxy(int xl, int yl) void outputxyO const Ivate *x,*y, };
类中的数据成员也可以是指向堆中某一内存单元的指针,内存 单元可以由构造函数分配,析构函数来释放 //exp4_9 #include <iostream.h> class A { public: A(int x1=0,int y1=0); ~A(); void setxy(int x1,int y1); void outputxy() const; private: int *x,*y; };
/exp4 9.cpp #include"exp4 9. h' A: A(int xl, int yl) x= new int(xl) y= new int(y1) A: : AO delete x. delete y void A: Setxy(int xl, int yl) *x=x1;
//exp4_9.cpp #include "exp4_9.h" A::A(int x1,int y1) { x = new int(x1); y = new int(y1); } A::~A() { delete x; delete y; } void A::setxy(int x1,int y1) { *x = x1; *y = y1; }