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 . . .

猜你喜欢

转载自www.cnblogs.com/zhuzhenwei918/p/9201775.html