Multi-Dimensional arrays

Nested/Multi-D arrays

Multi-dimensional arrays are essentially nested arrays (An array that contain other arrays)

  • T[k][k] : Two-Dimensional, Fixed-size

  • T[ ][ ] : Two-Dimensional, Dynamic-size

  • T[ ][k] or T[k][] : Two-Dimensional, Mixed-size

Multi-dimensional arrays can have any level of nesting

  • T[2][2][2] : Three-Dimensional, Fixed-Size (all k are the same)

  • T[2][8][4][12] : Four-Dimensional, Fixed-Sizes ( k‘s are of different values)

  • T[ ][ ][ ][ ][ ] : Five-Dimensional, Dynamic-Size

  • T[ ][3][2][ ][9][ ] : Six-Dimensional, Mixed-Size

  • You cannot have different types within nested arrays

  • The maximum level of nesting for nested arrays is 15. If you try to create a variable with 16 nested arrays, you will get a ‘stack too deep’ error. As stack pointer in the EVM cannot access a slot in the stack that is deeper than is 16th element (top down).

Indexing and Referencing

Things may be a little counter-intuitive because the X & Y axis are reversed.

bool[2][] flags;
  • top-level is a dynamic array, with any number of elements

  • each nested element is a fixed-array of size 2

So, to reference such things, the dynamic dimension is high order.

  • When declaring, we are declaring in reverse, from bottom to the top-most level.

  • When slicing/indexing, we operate normally, from top down.

Mixed-size: nested dynamic

Fixed-size applied to the top level of nesting. The nested elements can be of any length.

  • [ [0,1], [2], [3], [7,8,9] ]

There can only be 4 top level elements, where each element is a dynamic array.

Storage

  • Can add as many nested elements as needed. no restrictions.

  • If its a storage array, we can use push to add new elements.

  • values[0] = [0, 1, 13]

Memory

  • push is not available

  • Must init the nested dynamic array with a specified length.

  • However, each nested array can have a different length.

  • "Free-sizing" arrays are not allowed in memory - their lengths must be defined and unchanged for memory allocation.

  • values[0] is of length 2

  • values[1] is of length 3

Use loop to populate elements of a nested array

  • nodes and tree[0] are connected by the same memory reference

  • any changes made to one, is reflected in the other, as the same memory location is being updated.

  • tree[0] = [6,6,6] = nodes

  • Dynamic nested arrays in memory are possible contingent on the fact that their lengths are defined before assignment.

  • This reduces them to a fixed-size array.

  • However, they are "dynamic" in that each nested array could be of different length.

Mixed-size array, top dynamic

Top level is dynamic, can take as many elements as needed. However, each element must be an array of size 2.

Storage

  • Cannot values.push([3, 4 , 5]); as its a fixed-array with 3 elements.

  • Cannot values[3] = [7, 8]; must use push method.

Memory

  • The top level is dynamic and its length is undefined;

  • We must first define it.

  • In doing so it becomes a nested fixed-fixed array.

This might seems redundant. But who knows?

Nested fixed-fixed

  • 2 top-level elements

  • each element can be an array of fixed size 4

  • [ [1,2,3,4], [5,6,7,8] ]

Example 4: nested dynamic

  • tree.length = 0

  • tree[0].length will revert.

Last updated