如下:
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
29
30
31
32
|
#include <iostream> #include <windows.h> using namespace std; int main() { cout << "create dynamic two-dimension array..." << endl; int sizeX = 5; int sizeY = 8; // 申请 double ** array = new double *[sizeX]; for ( int i = 0; i < sizeX; i++) { array[i] = new double [sizeY]; } for ( int i = 0; i < sizeX; i++) { for ( int j = 0; j < sizeY; j++) { array[i][j] = i + j; } } for ( int i = 0; i < sizeX; i++) { for ( int j = 0; j < sizeY; j++) { cout << array[i][j]; } cout << endl; } // 释放 for ( int i = 0; i < sizeX; i++) { delete [] array[i]; } delete [] array; system ( "pause" ); return 0; } |
如上,结果如下:
create dynamic two-dimension array...
01234567
12345678
23456789
345678910
4567891011
Press any key to continue . . .
总结
以上所述是小编给大家介绍的c++创建二维动态数组与内存释放问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.mamicode.com/info-detail-2342340.html