【问题标题】:How to use 'new' instead of 'malloc' to allocate a 2D-array, dynamically?如何使用“new”而不是“malloc”动态分配二维数组?
【发布时间】:2015-08-08 10:56:55
【问题描述】:

我想用二维指针制作矩阵。

当我使用 'malloc' 和 'free' 函数来使用内存时没有问题(请参阅我的代码)。 但是,我无法使用“new”和“delete”编写相同的代码。

如您所知,一维指针可以由“new”声明。例如,

double *example = new double [10];
delete [] example;

那么,如何使用'new'声明二维指针?

    double **matrix;    // 2-D pointer which represents entries of a matrix
    int row, column;    // the number of rows and column of the matrix
    int i;

    // set the size of the matrix
    row = 3;
    column = 5;

    // allocate memories related to the number of rows
    matrix = (double **)malloc(sizeof(double *) * row);

    // allocate memories related to the number of columns of each row
    for(i = 0; i < row; i++)
    {
        matrix[i] = (double (*))malloc(sizeof(double) * column);
    }

    // example: use of matrix
    matrix[2][4] = 10.5;

    // return used memories
    free(matrix);

【问题讨论】:

  • @minitech:你在 cmets 中回答,mod? :(

标签: c++ pointers multidimensional-array malloc


【解决方案1】:

嗯,直接等效是这样的:

// allocate memories related to the number of rows
double** matrix = new double*[row];

// allocate memories related to the number of columns of each row
for(i = 0; i < row; i++)
{
    matrix[i] = new double[column];
}

// usage here

// de-allocate memories related to the number of columns of each row
// (YOU FORGOT THIS IN YOUR ORIGINAL CODE!!!)
for(i = 0; i < row; i++)
{
    delete matrix[i];
}

delete[] matrix;

不过,真的,你不想要这个。这是一团糟,几乎没有内存位置。

更不用说手动内存管理是完全容易出错的,你的原始代码中有rowdoubles 的泄漏就证明了这一点。

这是怎么回事:

struct Matrix
{
    Matrix(const unsigned int row, const unsigned int column)
       : row(row)
       , column(column)
       , data(row*column, 0)
    {}

    double& at(const unsigned int y, const unsigned int x)
    {
        return data[y + x*row];
    }

private:
    const unsigned int row, column;
    std::vector<double> data;
};

它使用vector 来避免任何 讨厌的内存管理,并将二维索引访问包装在实际上是单个数据缓冲区的地方,这样你就没有n 指针间接。

您可以根据需要将布局调整为行优先或列优先。

【讨论】:

  • 为什么不添加double *operator[](const unsigned int y){return data.data()+y*row;},来模拟索引:)
【解决方案2】:

您不必单独分配列。一大块就够了,也方便删除。

分配:

double** matrix = new double* row;
double*  ptr = new double [row * column];
for ( int i = 0; i < row; i++, ptr += column )
    matrix[i] = ptr;

免费:

delete [] matrix[0];
delete [] matrix;

【讨论】:

    【解决方案3】:

    分配,

    double** matrix = new double*[row];
    for(size_t i = 0 ; i < row ; ++i)
        matrix[i] = new double[column];
    

    要取消分配,

    for(size_t i = 0 ; i < row ; ++i)
        delete matrix[i];
    delete[] matrix;
    

    【讨论】:

    • 啊!没有执行。我的错。已编辑。
    【解决方案4】:

    我的方法与其他解决方案略有不同。该函数采用 3 个参数,3D 指针(double 由一个指针指向,该指针指向一个指针,该指针指向一个指针,该指针指向一个指针 x),rows 和 @987654325 的大小@ 是 size_t (索引的有符号值是开销)。它只允许通过间接引用函数来使用在 main() 中定义的 2D 指针变量。顺便说一句,这将通过使用 double**&amp; x 来完成。

    #include <iostream>
    
    size_t const SIZE { 2 };
    
    void Alloc2D(double ***x, size_t row, size_t col);
    void Alloc2D(double ***x, size_t row, size_t col)
    { 
        *x = new double*[row];
        for(size_t i {}; i < row; i++)
        {
            (*x)[i] = new double[col];
        }
    }
    
    int main()
    {
        double** matrix;
    
        // 2 x 2 matrix
        Alloc2D(&matrix, SIZE, SIZE);
        matrix[0][0] = 9;
        matrix[0][1] = 8;
        matrix[1][0] = 7;
        matrix[1][1] = 6;
    
    
        for(size_t i {}; i < SIZE; i++)
            delete matrix[i];
        delete[] matrix;
    }
    

    Run on IDE

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 2014-10-23
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多