ReasonJun

Solidity : storage, memory, calldata, stack 본문

Blockchain/Solidity

Solidity : storage, memory, calldata, stack

ReasonJun 2023. 10. 15. 00:54
728x90

Storage

Storage is a persistent data store in Solidity that is used to store state variables. State variables are variables that are saved to the blockchain and can be accessed and modified by any user of the contract. Storage is the most expensive type of storage in Solidity, as it requires gas to read and write to.

 

Memory

Memory is a temporary data store in Solidity that is used to store data during contract execution. Anything that does not need to be saved to the blockchain, including function arguments and local variables, is stored in memory. Memory is less expensive than storage, as it does not require gas to read and write to.

 

Calldata

Calldata is a read-only data store in Solidity that is used to store function arguments and transaction data. Calldata is the data that is passed to a contract when a function is called. It is important to note that calldata cannot be modified.

 

Stack

The stack is a temporary data store in Solidity that is used to store local variables and function calls. The stack is used to keep track of the execution state of a contract. The stack is the least expensive type of storage in Solidity, as it does not require gas to read and write to.

 

Data Location Purpose Read-only? Persistent?
Storage Stores state variables No Yes
Memory Stores temporary variables No No
Calldata Stores function arguments and transaction data Yes No
Stack Stores local variables and function calls No No

 

Example

The following Solidity code example shows how to use storage, memory, and calldata:

contract MyContract {
  // State variable stored in storage
  uint256 public myVariable = 10;

  // Function that takes a uint256 as a memory argument and returns the sum of myVariable and the argument
  function add(uint256 memory myArgument) public returns (uint256) {
    // Read the value of myVariable from storage
    uint256 myStorageVariable = myVariable;

    // Add the value of myArgument to the value of myStorageVariable
    uint256 result = myStorageVariable + myArgument;

    // Return the result
    return result;
  }
}

In this example, the myVariable state variable is stored in storage. The myArgument function argument is stored in memory.

 

When the add() function is called, the EVM will first load the value of myVariable from storage into memory. Then, the EVM will add the value of myArgument to the value of myVariable in memory. Finally, the EVM will return the result of the addition.

 

It is important to note that the value of myVariable in storage is not modified by the add() function. This is because the add() function only operates on the value of myVariable in memory.

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : constant  (0) 2023.10.15
Solidity : Data Type (1)  (0) 2023.10.15
Solidity : Variable  (0) 2023.10.15
Solidity : Smart Contract rule  (0) 2023.10.14
Solidity : What is Solidity ?  (0) 2023.10.13
Comments