Storage and Memory Types in Solidity Programming

Share:
Storage and Memory Types in Solidity Programming

In this article, I will reveal all types of memory using in Solidity, the difference between them and show how to use it with maximal efficiency.

In this article, I will reveal all types of memory in Solidity programming, the difference between them and I will show how to use it with maximal efficiency. Also, let's consider the simplest smart contract with memory types using.


Storage


As you know from the previous article there are three types of memory in Solidity language. All of them represent three different areas in Ethereum Virtual Machine (EVM).

The first one is Storage

Storage stays the same aside function calls and transactions. It means that data put to storage will be saved in the Ethereum network and will be opened for access by other users. Storage is mostly used for storing important data that needs to stay ‘alive’ outside the function and contract it was created in. To give an illustration it can be used for storing a list of users who need to be paid with dividends according to their investment. It is extremely important data that needs to be stored globally in the system in order not to be lost. 

The maximum size of the data unit called is 256 bit. And the minimum is 8 bit.

Accessing storage memory type is extremely expensive as you can remember from the previous article.


Memory


The next data area in Ethereum Virtual Machine is a memory. For memory smart contract gets a fresh instance on any new message call. The lager data value gets the more expensive it becomes (it scales quadratically). An example of usage memory data type is next: a local variable in contract function that is only needed for calculations inside this function, data of this variable will be stored at the time of function executing and no longer. After the function is finished data will be cleared. 

The data size of the memory word is the same as the storage one. (from 8 bit to 256 bit).


Stack


The EVM is a stack machine which means that all computations are happening in a data structure called a stack. The stack consists of words of 256 bit each and has a maximum size of 1024 elements. Access to the stack is limited to the top end in the following way: it is possible to copy one of the topmost 16 elements to the top of the stack or swap the topmost element with one of the 16 elements below it. All other operations can access a few topmost elements depending on the exact operation you want to call. Full access to the stack can be gained by moving all elements from it to memory or storage. But it is not a common practice.


Solidity easiest examples


Let’s now write the easiest Solidity code example and discover the usage of different memory types in practice.


pragma solidity 0.5.8;

contract Test { uint256 private someNumber;

function set(uint256 number) public {
    someNumber = number;
}

function get() public view returns(uint256) {
    return someNumber;
}

}


Here we have an example of a smart contract, let’s figure it out line by line.


pragma solidity ^0.5.6;

The first line is the Solidity compiler version.

It can be used with a special symbol which will mean compiler of the specified version or higher.


contract Test {

The next line says that the contract named ‘Test’ was created. Any contract can be inherited just like a class in OOP (object-oriented programming) languages. But we will talk about it later.


uint256 private someNumber;

The next line is declaring a new variable. It has type uint256 which means that it is the 256-bit size and can store the values from 0 to 2^256. Private is a visibility modifier that shows that this variable cannot be accessed from outside this contract, it was done to make get() function make some sense, otherwise, in case it was defined as public there would be automatically generated getter function.

And most important it is a storage variable because it is defined outside any function. It is also can be called a global variable. On this line, the storage variable declaration will cost still nothing because any data part in solidity is set to zero-value by default and only changing from zero-value to non-zero needed to be paid.


function set(uint256 number) public {
someNumber = number;
}

The next one is a function declaration. Besides the ordinary function declaration here we have a ‘public’ modifier. In Solidity, it means that this function can be called outside the contract, and not only even by another contract but also by any account. So anyone in the system can change the value of ‘someNumber’ unless we make some restrictions. Another key thing to remember is that any function call in Solidity is a transaction. In order to notify the system that transaction is a contract function call, we need to fill specific field ‘Input data’ when sending the transaction.

In case this function is called with the non-zero value it will write a 256-bite value to the storage variable which means that the address which sent a transaction with a function call needs to pay 20 000 gas fee for changing one storage value from zero to non-zero + 21 000 for executing transaction and some additional expenses for calling contract function, in summary, it is 41 684 gas.



function get() public view returns(uint256) {
return someNumber;
}

}

Let’s move to get() function. It is public like the previous one and also it has few differences. 

The first one is the ‘view’ modifier. It means that in this function we will not change any storage data. The detailed explanation I will bring in the next articles.

The second one is ‘returns’. Here we need to specify the variable type that we will return from the function. The cost of this function call will be next= 200 gas for reading storage value + 21 000 for executing the transaction and again some for calling contract function. Summary 21 698.

That is a complete smart contract that can be deployed to the system and can be working but it has no actual use yet. In the next article, I will create a more real and difficult contract and get through it.


Armen Arakelyan



Comments

Scroll to top