c++创建二维动态数组与内存释放

时间:2022-02-01 16:39:49

如下:

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "create dynamic two-dimension array..." << endl;
int sizeX = ;
int sizeY = ;
// 申请
double** array = new double*[sizeX];
for (int i = ; i < sizeX; i++) {
array[i] = new double[sizeY];
} for (int i = ; i < sizeX; i++) {
for (int j = ; j < sizeY; j++) {
array[i][j] = i + j;
}
} for (int i = ; i < sizeX; i++) {
for (int j = ; j < sizeY; j++) {
cout << array[i][j];
}
cout << endl;
} // 释放
for (int i = ; i < sizeX; i++) {
delete[] array[i];
}
delete[] array;
system("pause");
return ;
}

如上,结果如下:

create dynamic two-dimension array...

Press any key to continue . . .