Solidity Tutorial

Arrays in Solidity – Fixed Size Array, Dynamic Array, Storage Array & Memory Array

Share on twitter
Share on linkedin
Share on whatsapp
Share on facebook
Share on telegram

Contents

What is Array

An array is a data structure in almost all programming languages that hold elements (usually of the same type) across contiguous memory locations. It is advantageous to store a collection of elements of the same type in a single array variable than declaring one variable for each of them. The element inside the array can be accessed by an index which is the position of that element. The first element of the array is stored in the 0th position.

fixed sized array and dynamic array in solidity
(Source)

 

Arrays in Solidity

The Arrays in Solidity can be classified into the following two types based on size –

  • Fixed Size Array
  • Dynamic Array

Furthermore, the solidity array can also be categorized based on where they are stored as –

  • Storage Array
  • Memory Array

Let us understand each of them in more detail.

1. Fixed Size Array

In a fixed size array in Solidity, the size of the array has to be specified during declaration. Once declared, the array size cannot be changed. Any attempt to reference beyond the last array position will result in an error.

The fixed size array can be initialized either inline during declaration or after the declaration in another function as shown in the below example. A value can be assigned to a specific position of the array by using the index of that position as shown in the third initialization example.

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Inline Initialization
    uint[3] public age = [8,15,32];

    string[3] public name; 

    uint[4] public flag;

    function initializeName() public
    {
        name = ["Sunny","Bob","Lilly"];
    }

    function initializeFlag() public
    {
       flag[0] = 21;
       flag[1] = 34;
       flag[2] = 65;
       flag[3] = 12;
    }
    
}

 

2. Dynamic Array

In a dynamic array in Solidity, the size of the array is not mentioned during the declaration. The size of the dynamic array can be changed during the runtime as more elements are added to the array.

The below example shows different ways in which the dynamic array can be declared.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Dynamic Array
    uint[] public flag;

    // Dynamic Array with first 3 elements initialized
    uint[] public age = [8,15,32];

     // Dynamic Array initialized with new keyword
    int[] public num = new int[](5);

}

 

3. Storage Array

The storage array is persisted permanently in the blockchain storage. They are declared outside the function but can be accessed by any function within the contract. You also need to spend gas to work with storage arrays. Storage arrays can be both fixed size and dynamic. All the above examples of previous sections are of storage arrays.

4. Memory Array

The memory array is not stored on the blockchain but as the name suggests, it is stored in memory on a temporary basis. They are declared inside the function and their scope is internal to that function only. Since it is not stored on the blockchain it does not incur gas costs.

It should however be kept in mind that dynamic array cannot be created in memory, you can only create fixed size memory array.

The below example shows how to initialize the fixed size aray in solidity in memory of size 2.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{

  function initialize() public pure returns(uint)
    {
        uint[] memory val = new uint[](2);

         val[0] = 100;        
         val[1] = 99;

        return (val[0] + val[1]);       
    }
}

 

Array Methods in Solidity

The array in Solidity support some methods to manipulate the arrays. They are as follows –

  • length 
  • push() 
  • push(x)
  • pop()

length

This is available for both dynamic storage arrays & fixed size arrays. This is used to get the length or size of the array.

The below example shows how to use length with fixed size arrays. The output shows the length of the array which is 4 in our example.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Fixed Size array
    uint[4] age = [8,15,32,21];


    function getAgeLength() public view returns (uint)
    {
        return age.length;
    }

}

Output –

0:uint256: 4

 

This example shows the length of the dynamic array whose output is 3 over here.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Dynamic Array 
    uint[] flag = [54,87,14];

    function getFlagLength() public view returns (uint)
    {
        return flag.length;
    }

}

Output –

0:uint256: 3

 

push()

This is available for the dynamic storage array and is used to append a zero-initialized element at the end of the array.

In the below example, we have initialized an array called flag and also declared two state variables flagSize and flagLastValue to keep track of the size of the array and the value at its last position.

A function pushFlag() is created where using the push method a new zero initialized element is appended at the end of the array. Also, the values of flagSize and flagLastValue is updated.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Dynamic Array 
    uint[] flag = [54,87,14];

    uint public flagSize = flag.length;

    uint public flagLastValue = flag[flagSize-1];


    function pushFlag() public
    {
        flag.push();

        flagSize = flag.length;

        flagLastValue = flag[flagSize-1];
    }

}

 

Before calling the pushFlag() we can see that the value of flagLastValue is 14 and the value of flagSize is 3.

Output –

0:uint256: 14
0:uint256: 3

After calling the pushFlag() the push() function appends the new element at the end initialized with zero. The flagSize has now become 4 and flagLastValue of the new last element is 0.

Output –

0:uint256: 0 
0:uint256: 4

 

push(x)

This is available for the dynamic storage array and is used to append the value ‘x’ at the end of the array.

In the below example, we have modified the above example to use push(x) in pushFlag() function.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Dynamic Array 
    uint[] flag = [54,87,14];

    uint public flagSize = flag.length;

    uint public flagLastValue = flag[flagSize-1];


    function pushFlag(uint x) public
    {
        flag.push(x);

        flagSize = flag.length;

        flagLastValue = flag[flagSize-1];
    }

}

 

Before calling the pushFlag() we can see that the value of flagLastValue is 14 and the value of flagSize is 3.

Output –

0:uint256: 14
0:uint256: 3

We now call pushFlag() with parameter value 99. The new output shows that the value of flagLastValue becomes 99 and the value of flagSize becomes 4. This confirms that a new element 99 has been appended to the ‘flag’ array.

Output –

0:uint256: 99 
0:uint256: 4

 

pop()

This is available for the dynamic storage array and is used to remove the last element from the end of the array.

In the below example, a function popFlag() is created that used pop() function to remove the last element of the ‘flag’ array. After calling the popFlag() the output confirms that the size of array is reduced to 2 and its last value is 87.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
    // Dynamic Array 
    uint[] flag = [54,87,14];

    uint public flagSize = flag.length;

    uint public flagLastValue = flag[flagSize-1];


    function popFlag() public
    {
        flag.pop();

        flagSize = flag.length;

        flagLastValue = flag[flagSize-1];
    }

}

 

Output –

0:uint256: 87 
0:uint256: 2