Professional Documents
Culture Documents
allocation
C/C++4
heapfree store
C++
newdelete
=new ()
delete ;
1 int *pi=new int(0);
1new
2
(
)
3
(initializer)
new
(initializer)
int *pi=new int(0);
pi
pi
delete pi;
pi
dynamic memory
deallocationpi
new
1new
2
(
)
3
(initializer)
new
1
=new [];
2
delete [ ];
delete
delete [ ]
#include <iostream.h>
#include <string.h>
void main(){
int n;
char *pc;
cout<<""<<endl;
cin>>n; //n17
pc=new char[n]; //178
strcpy(pc,);//
cout<<pc<<endl;
delete []pc;//pcn
return ; }
1.n
delete []pcn
delete pc
2.char *pc1pc1=p
delete [] pc1C++
3.initializer
(1) 10
void main()
#include <iostream.h>
#include <math.h>
{
double TriangleArea( )
double dArea;
{
dArea=TriangleArea( );
int i;
if (dArea==-1) cout<<" The
double dTArea;
7 double* pds = new double[5];
program failed!"<< endl;
8 if (!pds)
else cout << "The area of
9 { cout << "Error memory allocation!"<<endl;
triangle is " <<dArea<<endl;
10
return -1;
}
11 }
pds[0]=0;
for(i=1;i<4;i++)
72
{
pds[0]+=pds[i]/2;
232
}
pds[4]= pds[0];
for(i=1;i<4;i++) pds[0] *= ( pds[4] - pds[i]); the side is:
3,4,54
dTArea=sqrt(pds[0]);
23 delete []pds;
return dTArea;
}
:
(1)int*ptr;//int
(2)char*ptr;//char
(3)int**ptr;//int*
int *
(4)int(*ptr)[3];//int()[3] //
int * a[2];2int * .
int * a[2]
a[0]= new int[3];
a[1]=new int[3];
delete a[0];
delete a[1];
delete [] ;
.
int * b=new int[10];
b;
delete [] ,
b
delete [] b3; //
delete [] b2; //
new [1] [2];
cp
float(*cp) [30] [20]; //
float (*bp) [20]; //
cp=new float [1] [20] [30];
bp=new float [30] [20];
600
3020
30
20
delete [] cp; //
delete [] bp; //
void main(){
double **data;
data = new double*[m]; //
if ((data ) == 0)
{ cout << "Could not allocate. bye ...";
exit(-1);}
for(int j=0;j<m;j++)
{ data[j] = new double[n]; //
if (data[j] == 0)
{ cout << "Could not allocate. Bye ...";
exit(-1);} } //
for (int i=0;i<m;i++)
for (int j=0;j<n;j++) data[i][j]=i*n+j;
display(data); //2
//3
for (int i=0;i<m;i++)
delete[] data[i];
//
delete[] data;
return;
}
de_allocate(data)
void de_allocate(double **data){
for (int i=0;i<m;i++) delete[] data[i];
delete[] data;
return; } VC++
NULL
pp
p
memory leaknew
delete delete
new
new
free store
7.1.2
new
deletee
CGoods *pc;
pc=new CGoods; //
//CGoods
.
delete pc; //
delete
deleteC++
new
class
class CGoods{
char Name[21];
int Amount;
float Price;
float Total value;
public:
CGoods(){}; //
}//
//
void main(){
int n;
CGoods *pc,*pc1,*pc2;
pc=new CGoods(200010118000);
//
pc1=new CGoods(); //
cout<<<<endl;
cin>>n;
pc2=new CGoods[n];
//n
delete pc;
delete pc1;
delete []pc2; }
7.1.3
1
2
3
4
class CGoods{
char *Name; //char Name[21] ?
int Amount;
float Price; float Total_value;
public CGoods(){Name=new char[21];}
CGoods(CGoods & other){ //
this->Name=other.Name;
this->Amount=other.Amount;
this->Price=other.Price;
this->Total_value=other.Total_value;}
~CGoods(){delete Name;}//
}; //
void main(){
CGoods pc; //
CGoods pc1(pc); //
} //pc1pc
Name
other
pc
7.1
Name
other
pc
Name
*this
pc1
delete Name;
pc1pc
Name
pc
Name
pc
Name
pc1
7.1
CGoods(CGoods & other){ //
this->Name=new char[21];
strcpy(this->Name,other.Name);
this->Amount=other.Amount;
this->Price=other.Price;
this->Total_value=other.Total_value;}
Name
pc
7.1
Name
pc
Name
pc1
[7.3]
//
copy structor
class student{
char *pName; //
(copy Assignment
public:
Operator
student();
student(char *pname);
student(student &s); //
~student();
student & operator=(student &s);
//
};
//
student::student()
{ pName=NULL; cout<<Constructor\n"; }
//
student::student(char *pname){
if(pName=new char[strlen(pname)+1])
strcpy(pName,pname);
cout <<"Constructor" <<pName<<endl;}
//
student::student(student &s){
if(s.pName!=NULL){
if(pName=new char[strlen(s.pName)+1])
strcpy(pName,s.pName); }
//1
else pName=NULL;
cout <<"Copy Constructor" <<pName<<endl;}
//
student::~student(){
cout<<"Destructor"<<pName<<endl;
if(pName) delete[ ]pName;} //
//
student & student::operator=(student &s){
if(pName) delete[ ]pName; /*
*/
if(s.pName){
if(pName=new char[strlen(s.pName)+1])
strcpy(pName,s.pName);}
else pName=NULL;
cout <<"Copy Assign operator" <<pName<<\n;
return *this;}
VC++