Dynamic array in Solidity

A dynamic array is a type of array in which the length of the array can be changed at runtime. This means that the size of a dynamic array can be adjusted to fit the amount of data it needs to store, allowing for efficient use of memory. In contrast, a static array has a fixed length that is determined at compile time and cannot be changed at runtime.

Advantages of dynamic arrays

One advantage of using dynamic arrays is that they can save memory compared to static arrays. This is because a static array must allocate memory for its maximum size, even if it is not currently storing that many elements. For example, if a static array has a length of 100 but is only currently storing 5 elements, it will still take up memory for the other 95 unused elements. In contrast, a dynamic array will only allocate the memory it needs for the elements it is currently storing, allowing for more efficient use of memory.

Another advantage of dynamic arrays is that they can easily grow or shrink to fit the data they are storing. Because the length of a dynamic array is not fixed at compile time, you can change it at runtime. This makes it easy to add or remove elements from a dynamic array without having to worry about running out of space or wasting memory.

Dynamic arrays example

Here is an example of a dynamic array in the Solidity programming language:

pragma solidity ^0.8.0;

contract DynamicArrayExample {
    // Define a dynamic array of integers
    int[] dynamicArray;

    function addElement(int element) public {
        // Add an element to the end of the array
        dynamicArray.push(element);
    }

    function removeElement(int index) public {
        // Remove an element from the array
        delete dynamicArray[index];
    }

    function getArrayLength() public view returns (uint) {
        // Return the length of the array
        return dynamicArray.length;
    }
}

Try it in Remix

In this example, the dynamicArray variable is defined as a dynamic array of integers using the int[] syntax. The contract includes three functions: addElement, which adds an element to the end of the array; removeElement, which removes an element from the array at a given index; and getArrayLength, which returns the length of the array.

How are dynamic arrays implemented?

Dynamic arrays in Solidity are implemented using mappings under the hood. When you delete an element from a dynamic array, the memory it occupies is not actually freed. Instead, the element is simply marked as deleted and will not be returned when you access the array. As a result this can cause “looping holes,” where deleted elements can be accessed if you loop through the array in a certain way. To avoid this, manage the length of a dynamic array properly and keep track of which elements you’ve deleted.

In conclusion, dynamic arrays are a useful data structure in Solidity that allows for efficient use of memory and the ability to easily add or remove elements. However, it is important to understand how they are implemented and to manage their length properly to avoid any potential issues.

Resources

Blockchain Networks

Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.

Ethereum test network configuration and test ETH faucet information
Optimistic Ethereum Mainnet and Testnet configuration, bridge details, etc.
Polygon network Mainnet and Testnet configuration, faucets for test MATIC tokens, bridge details, etc.
Binance Smart Chain Mainnet and Testnet configuration, faucets for test BNB tokens, bridge details, etc.
Fanton networt Mainnet and Testnet configuration, faucets for test FTM tokens, bridge details, etc.
Kucoin Chain Mainnet and Testnet configuration, faucets for test KCS tokens, bridge details, etc.

Web3 Software Libraries

You can use the following libraries to interact with an EVM compatible blockchain.

Nodes

Learn how to run a Geth node. Read getting started with Geth to run an Ethereum node.

Fix a transaction

How to fix a pending transaction stuck on Ethereum or EVM compatible chain

Next Review – Create a simple Solidity smart contract

Ledger Nano X - The secure hardware wallet