开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜