小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c++動態(tài)內(nèi)存分配

 行者花雕 2021-04-28

  下面隨筆是關(guān)于c++動態(tài)內(nèi)存分配。

動態(tài)申請內(nèi)存操作符 new

  • new 類型名T(初始化參數(shù)列表)

  • 功能:在程序執(zhí)行期間,申請用于存放T類型對象的內(nèi)存空間,并依初值列表賦以初值。

  • 結(jié)果值:成功:T類型的指針,指向新分配的內(nèi)存;失?。簰伋霎惓!?/span>

釋放內(nèi)存操作符delete

  • delete 指針p

  • 功能:釋放指針p所指向的內(nèi)存。p必須是new操作的返回值。

 1 //例1 動態(tài)創(chuàng)建對象舉例
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point {
 8 
 9 public:
10 
11 Point() : x(0), y(0) {
12 
13   cout<<"Default Constructor called."<<endl;
14 
15 }
16 
17 Point(int x, int y) : x(x), y(y) {
18 
19   cout<< "Constructor called."<<endl;
20 
21 }
22 
23 ~Point() { cout<<"Destructor called."<<endl; }
24 
25   int getX() const { return x; }
26 
27   int getY() const { return y; }
28 
29   void move(int newX, int newY) {
30 
31     x = newX;
32 
33     y = newY;
34 
35 }
36 
37 private:
38 
39 int x, y;
40 
41 };
42 
43 int main() {
44 
45   cout << "Step one: " << endl;
46 
47   Point *ptr1 = new Point; //調(diào)用默認構(gòu)造函數(shù)
48 
49   delete ptr1; //刪除對象,自動調(diào)用析構(gòu)函數(shù)
50 
51   cout << "Step two: " << endl;
52 
53   ptr1 = new Point(1,2);
54 
55   delete ptr1;
56 
57   return 0;
58 
59 }
 1 運行結(jié)果:
 2 
 3 Step One:
 4 
 5 Default Constructor called.
 6 
 7 Destructor called.
 8 
 9 Step Two:
10 
11 Constructor called.
12 
13 Destructor called.

分配和釋放動態(tài)數(shù)組

    • 分配:new 類型名T [ 數(shù)組長度 ]

      • 數(shù)組長度可以是任何表達式,在運行時計算

    • 釋放:delete[] 數(shù)組名p

      • 釋放指針p所指向的數(shù)組。
        p必須是用new分配得到的數(shù)組首地址。

 1 //例2 動態(tài)創(chuàng)建對象數(shù)組舉例
 2 
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 class Point { //類的聲明同例6-16,略 };
 8 
 9 int main() {
10 
11   Point *ptr = new Point[2]; //創(chuàng)建對象數(shù)組
12 
13   ptr[0].move(5, 10); //通過指針訪問數(shù)組元素的成員
14 
15   ptr[1].move(15, 20); //通過指針訪問數(shù)組元素的成員
16 
17   cout << "Deleting..." << endl;
18 
19   delete[] ptr; //刪除整個對象數(shù)組
20   
21   return 0;
22 
23 }
 1 運行結(jié)果:
 2 
 3 Default Constructor called.
 4 
 5 Default Constructor called.
 6 
 7 Deleting...
 8 
 9 Destructor called.
10 
11 Destructor called.

動態(tài)創(chuàng)建多維數(shù)組

new 類型名T[1維長度][2維長度]…;

  • 如果內(nèi)存申請成功,new運算返回一個指向新分配內(nèi)存首地址的指針。

  例如

  char (*fp)[3];

  fp = new char[2][3];

 1 //例3 動態(tài)創(chuàng)建多維數(shù)組
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int main() {
 8 
 9   int (*cp)[9][8] = new int[7][9][8];
10 
11   for (int i = 0; i < 7; i++)
12 
13     for (int j = 0; j < 9; j++)
14 
15       for (int k = 0; k < 8; k++)
16 
17         *(*(*(cp + i) + j) + k) =(i * 100 + j * 10 + k);
18 
19   for (int i = 0; i < 7; i++) {
20 
21     for (int j = 0; j < 9; j++) {
22 
23       for (int k = 0; k < 8; k++)
24 
25         cout << cp[i][j][k] << " ";
26 
27         cout << endl;
28 
29     }
30 
31     cout << endl;
32 
33   }
34 
35   delete[] cp;
36 
37   return 0;
38 
39 }

 

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多