我想用二维指针制作矩阵.
当我使用'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)