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.
Now, we will create a function that will try to modify the dynamic array using memory variable.
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.
Also, in our function we want to change the first element (the element with the index 0) of the array save1[0] to "Berlin".
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.
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".
领英推荐
The second element (index 1) is "Chisinau".
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.
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.
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.
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 .