Back to homepage

Solidity fundamentals: data location (storage vs memory)

Published

A quick review of the different places where you can store variables in Solidity: storage and memory


In Solidity, there are two locations where you can store variables: in memory and in storage.

Make sure you check the Solidity basic data types and complex data types articles

Most of the time you don't need to use these keywords because Solidity handles them by default but when dealing with complex data types like struts and arrays, you'll need to specify where you want to store them. Find below the difference between them:

Storage

  • Storage refers to variables stored permanently on the blockchain. State variables (variables declared outside of functions) are by default storage and written permanently to the blockchain.

Memory

  • Memory variables are temporary, and are erased between external function calls to your contract. Variables declared inside functions are memory and will disappear when the function call ends.

calldata is somehow similar to memory, but it's only available to external functions. You can read more about external functions here.

Writting data in the blockain is an expensive operation so the gass fees will be higher to process these kind of transactions.

The most common analogy for this is your computer's hard drive and RAM 😉 Find below an example:

contract PokemonContract {
    struct Pokemon {
        string name;
        uint power;
        uint stamina;
        uint level;
    }

    Pokemon[] pokemonCollection;

    function levelUp(uint _index) public {

        // Declaring this variable as `storage` means it'll actually a pointer
        // to  pokemonCollection[_index]
        Pokemon storage selectedPokemon = pokemonCollection[_index];

        // so updating it will cause that it will change
        // pokemonCollection[_index] on the blockchain.
        selectedPokemon.level += 1;

        // Using `memory` the variable will be a copy of the data
        Pokemon memory anotherPokemon = pokemonCollection[_index + 1];
        // updating it will not modify the data stored in the blockchain
        anotherPokemon.level += 1;
        // and the variable will be lost once the function execution ends

    }
}

Fix error data location must be storage or memory

Data location is one of the main differences between Solidity and other programming languages like JavaScript, and can be a little tricky at first but luckly for us, the Solidity compiler will also give you warnings to let you know when you should use one of these keywords.

Data location must be storage or memory

Data location must be "memory" or "calldata" for parameter in function, but none was given.

If you're getting any of these warning messages from the Solidity compiler, that means you forgot to specify the data location in any of your variables or function parameteres inside your contract 😉 As a rule of thumb, you'll need to specify the data location for string, array or struct variables.

TAGS


If you enjoyed this article consider sharing it on social media or buying me a coffee ✌️

Buy Me A Coffee