C++ new 動態記憶體配置用法與範例

本篇 ShengYu 介紹 C++ new 動態記憶體配置 / delete 釋放記憶體用法

以下內容將分為這幾部份,

  • new int 的用法
  • new int 一維陣列
  • new int 二維陣列
  • new struct 範例
  • new struct 一維陣列範例

new int 的用法

以下是動態記憶體配置一個 int 的範例

1
int *p = new int;

也可以加上小括號這樣寫,

1
int *p = new int();

如果要分成兩行寫的話,

1
2
int *p;
p = new int();

如果要順便設定這個 int 的初始值的話,可以在 int 的建構子傳入預設值,示範一下如果我要初始值為 5 的用法,

1
int *p = new int(5);

當變數用完後很重要的一件事就是將這個動態配置記憶體的 int 釋放,以下為釋放記憶體的寫法,

1
delete p;

來看看實際範例吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// g++ cpp-new-delete.cpp -o a.out
#include <iostream>
using namespace std;

int main() {
int *p = new int();
*p = 10;
cout << p << "\n";
cout << *p << "\n";
delete p;

int *p2 = new int(15);
cout << p2 << "\n";
cout << *p2 << "\n";
delete p2;

return 0;
}

輸出如下,

1
2
3
4
0x1590c20
10
0x1590c20
15

new int 一維陣列

這邊示範動態記憶體配置一個一維陣列的寫法,new 陣列是要用中括號 [],不要跟上述的小括號 () 混淆唷,這兩個可是天差地遠,

1
2
3
int *p = new int[3]; // 不要寫成 int *p = new int(3); 唷!
...
delete[] p;

如果要分成兩行寫的話,

1
2
3
4
int *p;
p = new int[3];
...
delete[] p;

再來看怎麼配置一維陣列時同時給初始值,

cpp-new-delete2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// g++ cpp-new-delete2.cpp -o a.out
#include <iostream>
using namespace std;

int main() {
int *p1 = new int[3](); // 0,0,0
int *p2 = new int[3]{1,2,3}; // 1,2,3

cout << p1[0] << "," << p1[1] << "," << p1[2] << "\n";
cout << p2[0] << "," << p2[1] << "," << p2[2] << "\n";

delete[] p1;
delete[] p2;

return 0;
}

輸出如下,

1
2
0,0,0
1,2,3

new int 二維陣列

動態配置二維陣列這個通常會在影像處理中使用到這個技巧,假設我們要配置 3 * 4 大小的二維陣列,注意在使用完該變數後還是要將其變數 delete 歸還記憶體,二維陣列怎麼 new 的,delete 時就怎麼 delete。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// g++ cpp-new-delete3.cpp -o a.out
#include <iostream>
using namespace std;

int main() {
int **p = new int*[3];
for (int i = 0; i < 3; i++) {
p[i] = new int[4]{0};
}

p[0][1] = 1;
p[1][1] = 2;
p[2][1] = 3;

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << p[i][j] << " ";
}
cout << "\n";
}

for (int i = 0; i < 3; i++) {
delete[] p[i];
}
delete[] p;

return 0;
}

二維陣列輸出結果如下,

1
2
3
0 1 0 0 
0 2 0 0
0 3 0 0

new struct 範例

這邊示範怎麼 new struct,以 POINT 為例,

cpp-new-delete4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// g++ cpp-new-delete4.cpp -o a.out
#include <iostream>
using namespace std;

struct POINT {
int x;
int y;
};

int main() {
POINT *p1 = new POINT;
POINT *p2 = new POINT();
//POINT *p3 = new POINT(3); // error 沒有對應的建構子
POINT *p4 = new POINT({10,20});
POINT *p5 = new POINT(){10,20};

cout << p1->x << "," << p1->y << "\n";
cout << p2->x << "," << p2->y << "\n";
cout << p4->x << "," << p4->y << "\n";

delete p1;
delete p2;
delete p4;

return 0;
}

輸出如下,

1
2
3
0,0
0,0
10,20

再舉個 STUDENT 例子,

cpp-new-delete5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// g++ cpp-new-delete5.cpp -o a.out
#include <iostream>
#include <string>
using namespace std;

struct STUDENT {
int id;
string name;
};

int main() {
STUDENT *p1 = new STUDENT;
STUDENT *p2 = new STUDENT();
//STUDENT *p3 = new STUDENT(3); // error 沒有對應的建構子
STUDENT *p4 = new STUDENT({10, "Amy"});

cout << p1->id << "," << p1->name << "\n";
cout << p2->id << "," << p2->name << "\n";
cout << p4->id << "," << p4->name << "\n";

delete p1;
delete p2;
delete p4;

return 0;
}

輸出結果如下,

1
2
3
0,
0,
10,Amy

new struct 一維陣列範例

這邊示範怎麼 new struct 一維陣列,以 POINT 為例,

cpp-new-delete6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// g++ cpp-new-delete6.cpp -o a.out
#include <iostream>
using namespace std;

struct POINT {
int x;
int y;
};

int main() {
POINT *p1 = new POINT[3];
POINT *p2 = new POINT[3]{{1,2}, {3,4}, {5,6}};

cout << p1[0].x << "," << p1[0].y << " "
<< p1[1].x << "," << p1[1].y << " "
<< p1[2].x << "," << p1[2].y << "\n";
cout << p2[0].x << "," << p2[0].y << " "
<< p2[1].x << "," << p2[1].y << " "
<< p2[2].x << "," << p2[2].y << "\n";

delete[] p1;
delete[] p2;

return 0;
}

輸出如下,

1
2
0,0  0,0  0,0
1,2 3,4 5,6

再舉個 STUDENT 例子,

cpp-new-delete7.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// g++ cpp-new-delete7.cpp -o a.out
#include <iostream>
#include <string>
using namespace std;

struct STUDENT {
int id;
string name;
};

int main() {
STUDENT *p1 = new STUDENT[3];
STUDENT *p2 = new STUDENT[3]{{10, "Amy"}, {11, "Sam"}, {12, "Tom"}};

cout << p1[0].id << "," << p1[0].name << "|"
<< p1[1].id << "," << p1[1].name << "|"
<< p1[2].id << "," << p1[2].name << "\n";
cout << p2[0].id << "," << p2[0].name << "|"
<< p2[1].id << "," << p2[1].name << "|"
<< p2[2].id << "," << p2[2].name << "\n";

delete[] p1;
delete[] p2;

return 0;
}

輸出結果如下,

1
2
0,|0,|0,
10,Amy|11,Sam|12,Tom

其它相關文章推薦
C/C++ 新手入門教學懶人包
C/C++ 3種字串轉數字的方法與範例
C++ virtual 的兩種用法
C/C++ 字串反轉 reverse
C/C++ call by value傳值, call by pointer傳址, call by reference傳參考 的差別
C++ 類別樣板 class template
std::sort 用法與範例
std::find 用法與範例
std::queue 用法與範例
std::map 用法與範例
std::deque 用法與範例
std::vector 用法與範例