If you come from a Java or C# perspective and want to create a multi-dimensional array in C or 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

In Java, creating a 3-dimensional array is as simple as saying

int[][][] array3D = new int[x][y][z];

You can then access the elements of the 3-dimensional array at array3D[i][j][k].

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++.

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):

C 3D Array

Implementing multi-dimensional arrays in C is very similar to C++, except that we use malloc()\free() stdlib methods instead of the new\delete keywords. The memory representation below is the same, but we are going to focus in this section on making the elements of the 3 dimensional array contiguous.

To do so, we start by allocating space for all array elements in one call to malloc.

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.

Note that if we wanted the same jagged implementation as in the C++ example above, we could ignore the allocation of allElements and change the line of code array3D[i][j] = allElements + (i * y * z) + (j * z); to array3D[i][j] = malloc(z * sizeof(int)). Below is a sample code for allocating, accessing and deallocating a 3 dimensional array in C.

I rescind that previous comment.. kind of
It did not work when I placed the allocation code in a separate function (I was passing a pointer int*** to the function but things weren’t working.. this is likely because I have not developed in C for quite awhile and am a little rusty).

The code did however work when I returned a pointer (int***) from the function back to main.

Almost every item we buy or own has a barcode printed on it. We see those barcodes every day, but still we don’t understand what they really mean or how to generate them. That’s why I’ve created an article on Code Project that explains what barcoding is and details the design and implementation of a Java program that generates a barcode label from a given UPC code.