current position:Home>Ethereum smart contract development: storage location and scope of variables in Solidity language

Ethereum smart contract development: storage location and scope of variables in Solidity language

2022-08-04 21:16:09StevenX5

In Solidity, some data types are reference types, such as:

  • Arrays (string and bytes are special arrays and reference types)
  • struct(struct)
  • mapping

When using reference types in Solidity, you must specify the location of the data.

Storage location

Variables declared in a contract have a storage location that indicates where the value of the variable is stored.

Solidity provides three types of storage locations:

  • storage
  • memory
  • calldata

storage

On-chain storage space.This storage location is used to store permanent data, which is valid as long as the data exists in the contract.The stored gas is high.

Where:

  • The storage method of the state variable is forced to be storage;
  • The storage method of local variables can be declared as storage, memory or calldata;
  • Mappings can only be stored in storage.

memory

Memory storage.That is, the data is stored in memory, and the data is only valid during its lifetime (during the function call).

calldata

Call data.A special read-only data location used to store arguments to function calls (both internal and external), similar to memory.The advantage of using the calldata variable is that it doesn't save a copy of the data into memory and ensures that the data is not modified.

Summary:

  1. State variables are stored in storage;
  2. The storage method of local variables can be declared as storage, memory or calldata;
  3. In public functions (public) and external functions (external), function parameters can only be stored in memory or calldata;
  4. In private and internal functions, the storage method of function parameters can also be storage;
  5. Mapping can only be stored in storage and cannot be defined inside a function. Mapping is generally declared as a state variable;
  6. It is generally recommended to declare the storage method of function parameters and local variables as memory.

Contract example

The following is an example contract to demonstrate the usage of the data storage location.

// SPDX-License-Identifier: MITpragma solidity ^0.8.13;// variable storage location and scopecontract DataStorages {// state variable is forced to storagestring public str;bytes public bs;uint[] public arr;mapping(address => uint) map;struct Student {string name;uint score;}Student[] public students;// In the public function (public) and the external function (external), the storage location can be declared as meomory and calldata// In private functions (private) and internal functions (internal), the storage location can also be declared as storage// storage location of the stringfunction setString(string memory _str) public {str = _str;}// storage location of bytesfunction setBytes(bytes memory _bs) public {bs = _bs;}// storage location of the arrayfunction setArray(uint[] memory _arr) public {for(uint i = 0; i < _arr.length; i++) {arr.push(_arr[i]);}}// storage location of the structurefunction setStruct(Student calldata _student) public {students.push(_student);}// In internal functions, variables can be declared as storagefunction getLength(uint[] storage _arr) internal view returns(uint) {return(_arr.length);}// call inner functionfunction test() public view returns(uint) {return getLength(arr);}}

**Output:**We compile, deploy and run this contract example in Remix, the execution result is as follows:
insert image description here

copyright notice
author[StevenX5],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/216/202208042050433089.html

Random recommended