1 #include<iostream> 2 using namespace std; 3 const int size = 10; 4 double buf[size] = {0}; 5 6 int main(int argv ,char * args[]) { 7 8 double *p1,*p2; 9 p1 = new double[size]; 10 p2 = new (buf)double[size]; 11 for (int i = 0; i < size; i++) { 12 p1[i]=p2[(i+2)%size]=i+2; 13 cout << "p1== " << &p1[i] << " p1[i]=="<<p1[i]<<" "<< endl; 14 } 15 cout << "---------------华丽丽的分割线-----------------------" << endl; 16 for (int i = 0; i < size; i++) { 17 cout << "p2== " << &p2[i] << " p2[i]==" << p2[i] << " " << endl; 18 } 19 cout << "---------------一大波内存袭来!-----------------------" << endl; 20 double *p3, *p4; 21 p3 = new double[size]; 22 p4 = new (buf)double[size]; 23 for (int i = 0; i < size; i++) { 24 p3[i] = p4[(i + 2) % size] = i + 2; 25 cout << "p3== " << &p3[i] << " p3[i]==" << p3[i] << " " << endl; 26 } 27 cout << "---------------华丽丽的分割线-----------------------" << endl; 28 for (int i = 0; i < size; i++) { 29 cout << "p4== " << &p4[i] << " p4[i]==" << p4[i] << " " << endl; 30 } 31 cout << "###################华丽丽的分割线########################" << endl; 32 //数组指针 33 double *p[] = {p1,p2,p3,p4}; 34 for (int i = 0; i < 4; i++) 35 for (int j = 0; j < size; j++) { 36 cout << "p["<<i<<"]== " << &p[i][j] << " p["<<i<<"]"<<"["<<j<<"]==" << p[i][j] << " " << endl; 37 } 38 39 for (int i = 0; i < 4; i++) { 40 //释放new创建的内存 41 if (p[i]!=buf) delete [] p[i] ; 42 } 43 cout << "内存释放之后" << endl; 44 for (int i = 0; i < size; i++) { 45 46 cout << "p1== " << &p1[i] << " p1[i]==" << p1[i] << " " << endl; 47 } 48 cin.get(); 49 return 0; 50 }
1 p1== 0038DC28 p1[i]==2 2 p1== 0038DC30 p1[i]==3 3 p1== 0038DC38 p1[i]==4 4 p1== 0038DC40 p1[i]==5 5 p1== 0038DC48 p1[i]==6 6 p1== 0038DC50 p1[i]==7 7 p1== 0038DC58 p1[i]==8 8 p1== 0038DC60 p1[i]==9 9 p1== 0038DC68 p1[i]==10 10 p1== 0038DC70 p1[i]==11 11 ---------------华丽丽的分割线----------------------- 12 p2== 0125C2D0 p2[i]==10 13 p2== 0125C2D8 p2[i]==11 14 p2== 0125C2E0 p2[i]==2 15 p2== 0125C2E8 p2[i]==3 16 p2== 0125C2F0 p2[i]==4 17 p2== 0125C2F8 p2[i]==5 18 p2== 0125C300 p2[i]==6 19 p2== 0125C308 p2[i]==7 20 p2== 0125C310 p2[i]==8 21 p2== 0125C318 p2[i]==9 22 ---------------一大波内存袭来!----------------------- 23 p3== 0038B1F8 p3[i]==2 24 p3== 0038B200 p3[i]==3 25 p3== 0038B208 p3[i]==4 26 p3== 0038B210 p3[i]==5 27 p3== 0038B218 p3[i]==6 28 p3== 0038B220 p3[i]==7 29 p3== 0038B228 p3[i]==8 30 p3== 0038B230 p3[i]==9 31 p3== 0038B238 p3[i]==10 32 p3== 0038B240 p3[i]==11 33 ---------------华丽丽的分割线----------------------- 34 p4== 0125C2D0 p4[i]==10 35 p4== 0125C2D8 p4[i]==11 36 p4== 0125C2E0 p4[i]==2 37 p4== 0125C2E8 p4[i]==3 38 p4== 0125C2F0 p4[i]==4 39 p4== 0125C2F8 p4[i]==5 40 p4== 0125C300 p4[i]==6 41 p4== 0125C308 p4[i]==7 42 p4== 0125C310 p4[i]==8 43 p4== 0125C318 p4[i]==9 44 ###################华丽丽的分割线######################## 45 p[0]== 0038DC28 p[0][0]==2 46 p[0]== 0038DC30 p[0][1]==3 47 p[0]== 0038DC38 p[0][2]==4 48 p[0]== 0038DC40 p[0][3]==5 49 p[0]== 0038DC48 p[0][4]==6 50 p[0]== 0038DC50 p[0][5]==7 51 p[0]== 0038DC58 p[0][6]==8 52 p[0]== 0038DC60 p[0][7]==9 53 p[0]== 0038DC68 p[0][8]==10 54 p[0]== 0038DC70 p[0][9]==11 55 p[1]== 0125C2D0 p[1][0]==10 56 p[1]== 0125C2D8 p[1][1]==11 57 p[1]== 0125C2E0 p[1][2]==2 58 p[1]== 0125C2E8 p[1][3]==3 59 p[1]== 0125C2F0 p[1][4]==4 60 p[1]== 0125C2F8 p[1][5]==5 61 p[1]== 0125C300 p[1][6]==6 62 p[1]== 0125C308 p[1][7]==7 63 p[1]== 0125C310 p[1][8]==8 64 p[1]== 0125C318 p[1][9]==9 65 p[2]== 0038B1F8 p[2][0]==2 66 p[2]== 0038B200 p[2][1]==3 67 p[2]== 0038B208 p[2][2]==4 68 p[2]== 0038B210 p[2][3]==5 69 p[2]== 0038B218 p[2][4]==6 70 p[2]== 0038B220 p[2][5]==7 71 p[2]== 0038B228 p[2][6]==8 72 p[2]== 0038B230 p[2][7]==9 73 p[2]== 0038B238 p[2][8]==10 74 p[2]== 0038B240 p[2][9]==11 75 p[3]== 0125C2D0 p[3][0]==10 76 p[3]== 0125C2D8 p[3][1]==11 77 p[3]== 0125C2E0 p[3][2]==2 78 p[3]== 0125C2E8 p[3][3]==3 79 p[3]== 0125C2F0 p[3][4]==4 80 p[3]== 0125C2F8 p[3][5]==5 81 p[3]== 0125C300 p[3][6]==6 82 p[3]== 0125C308 p[3][7]==7 83 p[3]== 0125C310 p[3][8]==8 84 p[3]== 0125C318 p[3][9]==9 85 内存释放之后 86 p1== 0038DC28 p1[i]==-1.45682e+144 87 p1== 0038DC30 p1[i]==-1.45682e+144 88 p1== 0038DC38 p1[i]==-1.45682e+144 89 p1== 0038DC40 p1[i]==-1.45682e+144 90 p1== 0038DC48 p1[i]==-1.45682e+144 91 p1== 0038DC50 p1[i]==-1.45682e+144 92 p1== 0038DC58 p1[i]==-1.45682e+144 93 p1== 0038DC60 p1[i]==-1.45682e+144 94 p1== 0038DC68 p1[i]==-1.45682e+144 95 p1== 0038DC70 p1[i]==-1.45682e+144
这样,可以对内存进行很好的利用,避免内存泄露。而导致系统运行越来越慢!
时间: 2024-10-17 08:43:33