If You Come From A Java or C
If You Come From A Java or C
C++, you'll soon figure out that multi-dimensional array allocation in C\C++ is not as simple,
plus you'll have to worry about deallocation since there is no garbage collector to do the work for
you. Below, I'll show four different sample codes showing how to work with a three dimensional
array in Java, C#, C++ and C, respectively.
Java 3D Array
Collapse
int[][][] array3D = new int[x][y][z];
You can then access the elements of the 3-dimensional array at array3D[i][j][k].
Sample Code
Collapse
public static void main(String[] args)
{
// Array 3 Dimensions
int x = 4, y = 5, z = 6;
// Array Iterators
int i, j, k;
// Allocate 3D Array
int[][][] array3D = new int[x][y][z];
System.out.println('\n');
}
}
C# 3D Array
In C#, the concept is almost the same as in Java. However, C# makes the distinction between
jagged and multi-dimensional arrays. Elements of a multi-dimensional array are stored in a
contiguous block in memory while elements of a jagged array are not. Java arrays are actually
jagged arrays, while C# supports both and allows you to choose which one you want based on
the syntax of your code. Note that multi-dimensional arrays are better (in most cases) than jagged
arrays, and that is considered a minus point for Java.
Using jagged arrays in C# is not as simple as in Java. It’s almost like the way we would
implement it in C++.
Collapse
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[3];
However, multi-dimensional arrays in C# are very simply to use. You can create a 3 dimensional
array as follows:
Collapse
int[,,] array3D = new int[x, y, z];
Sample Code
Collapse
static void Main(string[] args)
{
// Array 3 Dimensions
int x = 4, y = 5, z = 6;
// Array Iterators
int i, j, k;
// Allocate 3D Array
int[,,] array3D = new int[x, y, z];
C++ 3D Array
To create a multi-dimensional array in C++, we should change perspective a little bit and think
of creating arrays that point to other arrays, which point to other arrays, and so on. For example,
to create a 2x3x4 array in C++, we should imagine the implementation as follows:
For simplicity, we are doing the jagged implementation of the multi-dimensional array (address
of array3d[0][1][0] is not directly after array3d[0][0][3] in memory representation above).
In the next section, we will implement it in C the contiguous way. To allocate a jagged 2D array
in C++, one can write the following (compare to C# jagged above):
Collapse
int** jaggedArray = new int*[2];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[3];
The elements can be accessed as usual: jaggedArray[i][j]. The extra work we have to do in
C++ is to explicitly deallocate the array.
Collapse
delete[] jaggedArray[0];
delete[] jaggedArray[1];
delete[] jaggedArray;
See the code sample below to understand how we allocate and deallocate a 3 dimensional array
in C++.
Sample Code
Collapse
#include <iostream>
void main()
{
// Array 3 Dimensions
int x = 4, y = 5, z = 6;
// Array Iterators
int i, j, k;
// Allocate 3D Array
int ***array3D = new int**[x];
// Deallocate 3D array
for(i = 0; i < x; i++)
{
for(j = 0; j < y; j++)
{
delete[] array3D[i][j];
}
delete[] array3D[i];
}
delete[] array3D;
}
C 3D Array
Collapse
int *allElements = malloc(x * y * z * sizeof(int));
Next, we create the arrays of pointers, and point to the contiguous elements we’ve already
allocated.
Collapse
int ***array3D = malloc(x * sizeof(int **));
for(i = 0; i < x; i++)
{
array3D[i] = malloc(y * sizeof(int *));
Collapse
#include <stdio.h>
#include <stdlib.h>
void main()
{
// Array 3 Dimensions
int x = 4, y = 5, z = 6;
// Array Iterators
int i, j, k;
// Allocate 3D Array
int *allElements = malloc(x * y * z * sizeof(int));
int ***array3D = malloc(x * sizeof(int **));
printf("\n\n");
}
// Deallocate 3D array
free(allElements);
for(i = 0; i < x; i++)
{
free(array3D[i]);
}
free (array3D);
}
Source Code