c++创建二维动态数组与内存释放问题
时间:2022-12-11 11:53:16|栏目:C代码|点击: 次
如下:
#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 . . .
总结