creating two-dimensional array dynamically in continuous memory block
I was trying to create 2D array in a continuous memory block, but it is giving M continuous block, each of N size.
int **arr = new int*[M];
for (int i = 0 ; i < M ; i++ )
{
arr[i] = new int[N];
}
How to create 2D 开发者_高级运维array in a continuous memory block?
int *buffer=new int[M*N];
int **arr=new int*[M];
for(int i=0;i<M;++i)
arr[i]=buffer+i*N;
Actually it's not necessary to store arr pointers - they can be calculated when needed.
精彩评论