Can we dynamically allocate 2D array without using any for loop or while loops? i there any direct command or function in c c++?
-
1Umm....yes? I mean, you don't ever need to use a loop to dynamically allocate a 2d array. – Jim Buck Sep 28 '12 at 07:08
-
1See [this answer](http://stackoverflow.com/a/12633765/968261), the code in it. – Alexey Frunze Sep 28 '12 at 07:52
6 Answers
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.

- 5,412
- 4
- 28
- 68
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]

- 6,518
- 10
- 53
- 80
-
Yeah but effective syntax is still of a 1D array and it cannot be accessed using [][] or *(*(arr+row)+col) – fkl Sep 28 '12 at 07:30
-
2And in fact this is almost always what you want when talking about a 2D array in constrast to an array of arrays (which is probably what the OP confused with a 2D array). – Christian Rau Sep 28 '12 at 07:34
-
If you want to avoid raw pointers, then `vector
a(R*C)` obviously has the same effect. – Fred Foo Sep 28 '12 at 08:26
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.

- 7,505
- 5
- 46
- 65
-
Pseudo 2D array is not the only way. You can still allocate without loop provided one dimensions value is determined at compile time. – fkl Sep 28 '12 at 07:32
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.

- 6,701
- 4
- 19
- 28
Try to replace loop by recursion

- 1,506
- 13
- 27
-
memset does not allocate memory. It only fills a memory block with some value – fkl Sep 28 '12 at 07:05
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";
}

- 199
- 10
-
2A choice? Yes. A reasonable choice considering any possible way one can think about the definition of array? **Definitely not!** – Christian Rau Sep 28 '12 at 07:31
-