Dynamically allocate 2D array without using any loops?

末鹿安然 提交于 2019-12-11 08:42:41

问题


Can we dynamically allocate 2D array without using any for loop or while loops? i there any direct command or function in c c++?


回答1:


Without using a loop you will have one restriction in ISO c++ i.e. size of one dimension has to be determined at compile time. Then this allocation can be done in a single statement as follows:

#define COLUMN_SIZE 10 // this has to be determined at compile time
int main()
{
    int (* arr)[COLUMN_SIZE];
    int rows = 20; // this is dynamic and can be input from user at run time
    arr = new int[rows][COLUMN_SIZE];
    arr[3][4] = 10;
    cout << arr[3][4] << endl;
    return 0;
}

The memory allocated with new needs to be freed. Also if we extend it to n dimensions, only one of these dimensions can be determined at run time. The reason is that compiler has to know the size of each row in order to create a row of contiguous memory.




回答2:


Although you should avoid raw pointers, this should work->

int *myArray = new int[R*C];

Here R is number of rows and C is number of columns. Although it is really a 1D array, you can manipulate it as 2D array. For example, myArray[i][j] can be read as->

myArray[i*C + j]



回答3:


The only way to do it with out loops is to allocate a psuedo 2D array thus:

int *ary = new int[sizeX * sizeY];

but then accessing that is non standard & frankly ugly:

ary[y*sizeX + x]

If you want a "real" 2D array then your stuck with loop intialization:

int **ary = new int*[sizeY];
for(int i = 0; i < sizeY; ++i) {
    ary[i] = new int[sizeX];
}

But then you have to be careful about clean up:

for(int i = 0; i < sizeY; ++i) {
    delete [] ary[i];
}
delete [] ary;

So in my view

std::vector<std::vector < int> >  

is probably the simplest and safest way to go in a real world app.




回答4:


Alternative way to access in arr[..][..] format.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    int COL ;
    int ROW ;
    COL = 8;
    ROW = 12;
    int (*p)[COL];
    int *mem = (int*)malloc(sizeof(int)*COL*ROW);
    memset(mem,0,sizeof(int)*COL*ROW);
    p = (int (*)[10])mem;

    printf("0x%p\n", p);
    printf("0x%p %d\n", p+1, (((int)(p+1))-((int)p))/sizeof(int));

    mem[2*COL+0] = 1;
    printf("%d\n", p[2][0]);
    mem[2*COL+5] = 2;
    printf("%d\n", p[2][5]);
    mem[6*COL+7] = 3;
    printf("%d\n", p[6][7]);

    p[1][2] = 4;
    printf("%d\n", mem[1*COL+2]);

    free(p);

    return 0;
}

Of course, you can do int (*p)[COL] = (int (*)[COL]) malloc(sizeof(int)*COL*ROW); directly.




回答5:


A std::map<TypeDim1, std::map<TypeDim2, TypeContent> > might be a dynamically allocated choice to represent a 2D array.

#include <map>
typedef std::map<int, std::map<int, std::string> > array2dstring;

int main(int argc, char *argv[])
{
   array2dstring l_myarray2d;
   l_myarray2d[10][20] = "Anything"; 
}



回答6:


Try to replace loop by recursion



来源:https://stackoverflow.com/questions/12635066/dynamically-allocate-2d-array-without-using-any-loops

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