How to Create 2, 3 (or Multi) Dimensional Arrays in C/C++?


We often need to create multi-dimensional arrays. In C/C++, depending on the memory management, this can be categorised into static arrays and dynamic arrays.

How to Create 2 or 3 Dimensional Static Arrays in C/C++?

The static arrays in C/C++ are arrays where sizes/dimensions are pre-defined, or known at compilation time. For example:

1
int a[10] = {0};
int a[10] = {0};

The above allocates a static integer array which has 10 elements and all elements of the array are initialised with zeros. The elements can be accessed via a[0], a[1] to a[9] and you don’t need to manually de-allocate the array, as it is managed/destroyed automatically.

Similarly, the following declares a two dimensional static array in C/C++.

1
int a[2][2] = { {0, 1}, {2, 3} };
int a[2][2] = { {0, 1}, {2, 3} };

where you can access elements via a[0][0], a[0][1], a[1][0], a[1][2]. for higher dimensions, syntaxs are similar, for example, the following is for 3 dimensional static arrays in C/C++:

1
int a[2][2][2] = { {{0, 1}, {0, 1}}, {{0, 1}, {0, 1}}};
int a[2][2][2] = { {{0, 1}, {0, 1}}, {{0, 1}, {0, 1}}};

The static arrays are stored in memory continuously even for multi-dimensional arrays.

How to Create 2 or 3 Dimensional Dynamic Arrays in C/C++?

On the other hand, the dynamic arrays determine the sizes/dimensions at runtime. The arrays are dynamically allocated depending on the needs. For example, to allocate a dynamic array of integer type, to 10 elements at runtime, we can use this in C/C++:

1
int *a = new int[10];
int *a = new int[10];

The array elements are accessed in the same way as the static arrays. However, we need to manually de-allocate the array when we are no longer using it. Otherwise, there will be a memory leak.

1
delete a;
delete a;

If you prefer the C-style: we need to use more primitive allocation/de-allocation function:

1
2
int *a = (int*)malloc(sizeof(int)*10);
free(a);
int *a = (int*)malloc(sizeof(int)*10);
free(a);

In C++, we prefer the static_cast:

1
int *a = static_cast<int *>(malloc(sizeof(int) * 10));
int *a = static_cast<int *>(malloc(sizeof(int) * 10));

The int *a declares a pointer to the integer type, which is the start of the integer array – the array elements are stored one by one continuously in memory.

For multiple dimensional dynamic allocated arrays, the syntax is similar, however, you will need to manually allocate/de-allocate the higher dimensions.

1
2
3
4
5
6
// allocate first dimension
int **a = new int*[2];
// allocate second eimension
for (int i = 0; i < 2; ++ i) {
   a[i] = new int[2];
}
// allocate first dimension
int **a = new int*[2];
// allocate second eimension
for (int i = 0; i < 2; ++ i) {
   a[i] = new int[2];
}

There is one difference, for multi-dimensional dynamic array, the higher dimensions are not necessarily stored continuously in the same memory block. For example, the above, int **a declares a pointer that points to a few pointers that point to integers. a[0] and a[1] are two pointers which point to two separate integer arrays.

To de-allocate, we follow the similar method:

1
2
3
4
for (int i = 0; i < 2; ++ i) {
   delete a[i]; // or free(a[i]);
}
delete []a;
for (int i = 0; i < 2; ++ i) {
   delete a[i]; // or free(a[i]);
}
delete []a;

Each sub-dimension is de-allocated and followed by the delete [] which indicates the de-allocation of the entire array.

Using STL::vector to declare the multi dimension dynamic array

We can use STL::vector to create multi dimension dynamic array, for example:

1
vector<int> a = {0, 1, 2};
vector<int> a = {0, 1, 2};

The above creates a vector that holds 3 integers, which are 0, 1 and 2. The elements in a vector can also be accessed via [] index operator e.g. a[0] to a[2]. The following declares a vector of 100 elements and all initialised to 1.

1
vector<int> a(100, 1);
vector<int> a(100, 1);

You don’t need to de-allocate the vector as it is automatically taken care of. For 2 dimensional array that is declared via vector, you can do something like this:

1
2
3
int cols = 2;
int rows = 2;
vector< vector<int> > array(cols, vector<int>(rows, 1));
int cols = 2;
int rows = 2;
vector< vector<int> > array(cols, vector<int>(rows, 1));

And you can access them via indices as well, e.g. array[0][1], array[0][0].

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
735 words
Last Post: What Personal Data Might be Revealed While Surfing The Net
Next Post: VPS Tool/API Server Upgraded to Four Cores!

The Permanent URL is: How to Create 2, 3 (or Multi) Dimensional Arrays in C/C++?

Leave a Reply