3D / nD Arrays in Compilers

4 Dimensional array of type A[d1][d2][d3][d4]

  • Row major formula for n Dimensional arrays: Address(A[i1][i2][i3[i4] = Base Address + [i1 * d2 * d3 * d4 + i2 * d3 * d4 + i3 * d4 + i4] * Data Type Size

  • Column major formula: Address(A[i1][i2][i3][i4]) = Base Address + [i4 * d1 * d2 * d3 + i3 * d1 * d2 + i2 * d1 + i1] * Data Type Size

  • You can apply the Horner's rule to reduce the number of multiplications.

3D array of type int A[l][m][n]

  • Row mapping Address(A[i][j][k]) = Base Address + [i * m * n + j * n + k] * Data Type Size

  • Column mapping Address(A[i][j][k]) = Base Address + [k * l * m + j * l + i] * Data Type Size

For row major, left to right, for column major, right to left.