小编Sup*_*ary的帖子

如何使用'new'代替'malloc'动态分配2D数组?

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

当我使用'malloc'和'free'函数进行内存使用时没有问题(参见我的代码).但是,我无法使用'new'和'delete'编写相同的代码.

如您所知,1-D指针可以通过'new'声明.例如,

double *example = new double [10];
delete [] example;
Run Code Online (Sandbox Code Playgroud)

那么,如何使用'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 …
Run Code Online (Sandbox Code Playgroud)

c++ malloc pointers multidimensional-array

1
推荐指数
1
解决办法
3659
查看次数

标签 统计

c++ ×1

malloc ×1

multidimensional-array ×1

pointers ×1