c++ creates 2d dynamic arrays with memory release issues
- 2020-06-07 04:53:35
- OfStack
As follows:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "create dynamic two-dimension array..." << endl;
int sizeX = 5;
int sizeY = 8;
// To apply for
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;
}
// The release of
for (int i = 0; i < sizeX; i++) {
delete[] array[i];
}
delete[] array;
system("pause");
return 0;
}
As above, the results are as follows:
[
create dynamic two-dimension array...
01234567
12345678
23456789
345678910
4567891011
Press any key to continue . . .
conclusion