#include<iostream>
using namespace std;
const int size = ;
double buf[size] = {}; int main(int argv ,char * args[]) { double *p1,*p2;
p1 = new double[size];
p2 = new (buf)double[size];
for (int i = ; i < size; i++) {
p1[i]=p2[(i+)%size]=i+;
cout << "p1== " << &p1[i] << " p1[i]=="<<p1[i]<<" "<< endl;
}
cout << "---------------华丽丽的分割线-----------------------" << endl;
for (int i = ; i < size; i++) {
cout << "p2== " << &p2[i] << " p2[i]==" << p2[i] << " " << endl;
}
cout << "---------------一大波内存袭来!-----------------------" << endl;
double *p3, *p4;
p3 = new double[size];
p4 = new (buf)double[size];
for (int i = ; i < size; i++) {
p3[i] = p4[(i + ) % size] = i + ;
cout << "p3== " << &p3[i] << " p3[i]==" << p3[i] << " " << endl;
}
cout << "---------------华丽丽的分割线-----------------------" << endl;
for (int i = ; i < size; i++) {
cout << "p4== " << &p4[i] << " p4[i]==" << p4[i] << " " << endl;
}
cout << "###################华丽丽的分割线########################" << endl;
//数组指针
double *p[] = {p1,p2,p3,p4};
for (int i = ; i < ; i++)
for (int j = ; j < size; j++) {
cout << "p["<<i<<"]== " << &p[i][j] << " p["<<i<<"]"<<"["<<j<<"]==" << p[i][j] << " " << endl;
} for (int i = ; i < ; i++) {
//释放new创建的内存
if (p[i]!=buf) delete [] p[i] ;
}
cout << "内存释放之后" << endl;
for (int i = ; i < size; i++) { cout << "p1== " << &p1[i] << " p1[i]==" << p1[i] << " " << endl;
}
cin.get();
return ;
}
p1== 0038DC28 p1[i]==
p1== 0038DC30 p1[i]==
p1== 0038DC38 p1[i]==
p1== 0038DC40 p1[i]==
p1== 0038DC48 p1[i]==
p1== 0038DC50 p1[i]==
p1== 0038DC58 p1[i]==
p1== 0038DC60 p1[i]==
p1== 0038DC68 p1[i]==
p1== 0038DC70 p1[i]==
---------------华丽丽的分割线-----------------------
p2== 0125C2D0 p2[i]==
p2== 0125C2D8 p2[i]==
p2== 0125C2E0 p2[i]==
p2== 0125C2E8 p2[i]==
p2== 0125C2F0 p2[i]==
p2== 0125C2F8 p2[i]==
p2== 0125C300 p2[i]==
p2== 0125C308 p2[i]==
p2== 0125C310 p2[i]==
p2== 0125C318 p2[i]==
---------------一大波内存袭来!-----------------------
p3== 0038B1F8 p3[i]==
p3== 0038B200 p3[i]==
p3== 0038B208 p3[i]==
p3== 0038B210 p3[i]==
p3== 0038B218 p3[i]==
p3== 0038B220 p3[i]==
p3== 0038B228 p3[i]==
p3== 0038B230 p3[i]==
p3== 0038B238 p3[i]==
p3== 0038B240 p3[i]==
---------------华丽丽的分割线-----------------------
p4== 0125C2D0 p4[i]==
p4== 0125C2D8 p4[i]==
p4== 0125C2E0 p4[i]==
p4== 0125C2E8 p4[i]==
p4== 0125C2F0 p4[i]==
p4== 0125C2F8 p4[i]==
p4== 0125C300 p4[i]==
p4== 0125C308 p4[i]==
p4== 0125C310 p4[i]==
p4== 0125C318 p4[i]==
###################华丽丽的分割线########################
p[]== 0038DC28 p[][]==
p[]== 0038DC30 p[][]==
p[]== 0038DC38 p[][]==
p[]== 0038DC40 p[][]==
p[]== 0038DC48 p[][]==
p[]== 0038DC50 p[][]==
p[]== 0038DC58 p[][]==
p[]== 0038DC60 p[][]==
p[]== 0038DC68 p[][]==
p[]== 0038DC70 p[][]==
p[]== 0125C2D0 p[][]==
p[]== 0125C2D8 p[][]==
p[]== 0125C2E0 p[][]==
p[]== 0125C2E8 p[][]==
p[]== 0125C2F0 p[][]==
p[]== 0125C2F8 p[][]==
p[]== 0125C300 p[][]==
p[]== 0125C308 p[][]==
p[]== 0125C310 p[][]==
p[]== 0125C318 p[][]==
p[]== 0038B1F8 p[][]==
p[]== 0038B200 p[][]==
p[]== 0038B208 p[][]==
p[]== 0038B210 p[][]==
p[]== 0038B218 p[][]==
p[]== 0038B220 p[][]==
p[]== 0038B228 p[][]==
p[]== 0038B230 p[][]==
p[]== 0038B238 p[][]==
p[]== 0038B240 p[][]==
p[]== 0125C2D0 p[][]==
p[]== 0125C2D8 p[][]==
p[]== 0125C2E0 p[][]==
p[]== 0125C2E8 p[][]==
p[]== 0125C2F0 p[][]==
p[]== 0125C2F8 p[][]==
p[]== 0125C300 p[][]==
p[]== 0125C308 p[][]==
p[]== 0125C310 p[][]==
p[]== 0125C318 p[][]==
内存释放之后
p1== 0038DC28 p1[i]==-1.45682e+144
p1== 0038DC30 p1[i]==-1.45682e+144
p1== 0038DC38 p1[i]==-1.45682e+144
p1== 0038DC40 p1[i]==-1.45682e+144
p1== 0038DC48 p1[i]==-1.45682e+144
p1== 0038DC50 p1[i]==-1.45682e+144
p1== 0038DC58 p1[i]==-1.45682e+144
p1== 0038DC60 p1[i]==-1.45682e+144
p1== 0038DC68 p1[i]==-1.45682e+144
p1== 0038DC70 p1[i]==-1.45682e+144
这样,可以对内存进行很好的利用,避免内存泄露。而导致系统运行越来越慢!