如下:
#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++创建二维动态数组与内存释放问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!