Storage vs Memory in Solidity

Storage vs Memory in Solidity

Understanding the storage and memory is not always intuitive. Having a good understanding of storage versus memory is really important.

Let's create a contract with a public dynamic array of strings and initialize it with two values.

No alt text provided for this image

Now, we will create a function that will try to modify the dynamic array using memory variable.

No alt text provided for this image

The variable "save1" will be stored in memory, not in storage, and it is initialized with the state variable called "cities" (save1 = cities).

It is mandatory for an array, string, or struct to specify memory or storage. If I will delete the word "memory" we will see an error.

No alt text provided for this image

Also, in our function we want to change the first element (the element with the index 0) of the array save1[0] to "Berlin".

No alt text provided for this image

Next, we will create a similar function where we will change only the save1 array to be declared of type storage, storage being the location where it will be saved.

No alt text provided for this image

Note that in each function, we changed only the variable defined inside of the function and not the state variable defined at the contract level.

Now we will deploy the contract to test it. To get values of array we are using indexes. The first element (index 0) is "Paris".

No alt text provided for this image

The second element (index 1) is "Chisinau".

No alt text provided for this image

Next, we will call the first function (memorySave()) that one that used the memory variable. We see that there is not any error and we will check the array again. We will check if it has changed the state variable.

No alt text provided for this image


We can see that the state variable (cities) was not modified, since the first element (index 0) of the dynamic array remained unchanged. It wasn't changed to "Berlin", so the function worked on a copy, not on the state variable, on the original.

Now, we will call the second function (storageSave()), the one with the storage variable. Also, we can not see any errors. We are going to check the contents of the array. The first element (index 0) remained unchanged - "Paris", but the second element (index 1) was changed to "Berlin" as expected.

No alt text provided for this image

We noticed that the function changed the state variable, even though inside it we have only changed save1 that was defined inside the function. We did not touch cities variable. So, save1 is a reference to the same memory location where the state variable was saved and changing it also changes the state variable.

No alt text provided for this image

You have already noticed this warning that says "Function state mutability can be restricted to view". That is because the function does not change the blockchain or the state variable that are saved on it.

I uploaded this code to my GitHub account.

See you in the future articles about #solidity , #smartcontract , #blockchain ,#blockchaintechnology , #blockchaindeveloper , #cryptocurrencies , #cryptoassets , #cryptocurrency , #crypto , #web3 , #web3development .

要查看或添加评论,请登录

Ion Platon的更多文章

  • What is Mapping in Solidity?

    What is Mapping in Solidity?

    If you are coming from other programming languages, then is good to know that the solidity mapping is similar to a…

  • Struct and Enum in Solidity

    Struct and Enum in Solidity

    A struct is a collection of key->value pairs similar to a mapping, but the values can have different types. Structs…

  • What are Libraries in Solidity?

    What are Libraries in Solidity?

    Libraries are similar to Contracts, but are mainly intended for reuse. A Library contains functions which other…

  • What are Interfaces in Solidity?

    What are Interfaces in Solidity?

    Interfaces are similar to abstract contracts and are created using interface keyword. There are some characteristics…

  • What is Inheritance in Solidity?

    What is Inheritance in Solidity?

    Let's take a closer inspection at Inheritance in Solidity. Inheritance is an advanced concept in #solidity and also in…

  • The Restricted Access Pattern in Solidity

    The Restricted Access Pattern in Solidity

    The Restricted Access to a Contract is a common practice. By default, a contract state is read-only, unless it is…

  • Build First Cryptocurrency in Testnet using Solidity and Remix

    Build First Cryptocurrency in Testnet using Solidity and Remix

    Creating own currency is a great way not only to solidify our coding or solidify our knowledge of building of smart…

  • The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    The Fallback Functions in Solidity. How gas costs work in Smart Contracts.

    Let's take a look at Fallback Functions in solidity. We are going to open our eyes and become a little bit more…

  • Bytes vs Strings in Solidity

    Bytes vs Strings in Solidity

    Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any…

  • Loops in Solidity

    Loops in Solidity

    Looping is a type of functionality in coding, which is very handy. Loops allow you to iterate through data and take…

社区洞察

其他会员也浏览了