new matrix[N][N] failure [duplicate]

六眼飞鱼酱① 提交于 2020-01-03 05:35:05

问题


I'm having a stack overflow allocating a huge matrix on the stack (and I agree with that: it's stupid to allocate it there) and I'm writing the following code since I want to access the matrix's elements with the subscripts indices mat[x][y]

double (*mul1)[N][N];
mul1 = new double[N][N];

I'm receiving an error:

error C2440: '=' : cannot convert from 'double (*)[1000]' to 'double(*)[1000][1000]'

Why can't I allocate a bidimensional array with new?


回答1:


double *mul1[N];
for (int i=0;i<N;++i)
    mul1[i] = new double[N];

Representing a 2D array as a 1D array

Performance of 2-dimensional array vs 1-dimensional array




回答2:


You can do it like so:

int N = 10 ;
double** mul1 = new double*[N];
for(int i = 0; i < N; ++i)
   mul1[i] = new double[N];


来源:https://stackoverflow.com/questions/15586075/new-matrixnn-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!