header-langage
简体中文
繁體中文
English
Tiếng Việt
한국어
日本語
ภาษาไทย
Türkçe
Scan to Download the APP

Solidity extremely brief introduction | about 4: variable data storage and scope

2022-06-12 18:43
Read this article in 9 Minutes
总结 AI summary
View the summary 收起
4. Variable data storage and scope storage/ Memory /calldata
原文作者:0xAA


I have been reworking solidity to consolidate the details and also writing a "Solidity Minimalism Primer" for nerds (find another tutorial for programmers) with weekly updates of 1-3 sessions.


All open source code in: github.com/AmazingAng/WTFSolidity


Reference types in Solidity


Reference Type:Including array, struct and mapping, such variables take up a large space and directly pass the address (similar to Pointers) when assigning values. Because such variables are complex and occupy large storage space, we must declare the location of data storage when using them.


Data location


There are three types of solidity data storage locations: storage, Memory and CallData. Gas costs are different for different storage locations. Data of storage type is stored on the chain, which is similar to the hard disk of a computer and consumes a lot of gas. Memory and calldata types are temporarily stored in memory and consume less gas. General usage:


1. Storage: The default status variable in the contract is storage, which is stored on the chain.


2. Memory: Parameters and temporary variables in functions are generally stored in memory and not linked.


3. Calldata: Similar to memory, it is stored in memory without linking. Unlike memory, calldata variables cannot be immutable and are generally used as arguments to functions. Example:



Rules for assigning different types to each other


When different storage types assign values to each other, sometimes independent copies are made (changing a new variable does not affect the original variable), and sometimes references are made (changing a new variable does affect the original variable). Here are the rules:


1. When a storage is assigned to a local storage, a reference is created. Changing the new variable affects the original variable. Example:



1. Assigning a storage value to memory creates a separate copy. Modifying one copy does not affect the other. And vice versa. Example:



1. Assigning a value to memory creates a reference, and changing a new variable affects the original variable.


2. In other cases, an independent copy is created when a variable is assigned to the storage. Modifying one copy does not affect the other.


Scope of a variable


Solidity can be divided into three types of variables according to their scope: state variables, local variables and global variables.


1. State variables


状态变量是数据存储在链上的变量,所有合约内函数都可以访问 ,gas 消耗高。状态变量在合约内、函数外声明:



We can change the value of a state variable in a function:



2. Local variables


Local variables are variables that are valid only during the execution of a function. After the function exits, the variable is invalid. Local variable data stored in memory, not linked, gas low. Local variables are declared in functions:



3. Global variables


Global variables are global scoped working variables and are solidity reserved keywords. They can be used in functions without being declared:



In the example above, we used three common global variables: msg.sender, block.number, and msg.data, which represent the originating address, the current block height, and the request data, respectively. Here are some common global variables, see this for a more complete listlink:


· BlockHash (uint blockNumber): (bytes32) Hash value for a given block - only for 256 recent blocks, not the current block.


· Block. coinbase: (Address Payable) the address of the miner in the current block


· block.gaslimit: (uint) Gaslimit of the current block


· block.number: (uint) Number of the current block


· block.timestamp: (uint) Timestamp of the current block, in seconds since the Unix era


· Gasleft (): (uint256) Surplus gas


· msg.data: (bytes callData) Complete calldata


· MSG. Sender: (Address payable) Message sender (current caller)


· msG. sig: (bytes4) The first four bytes of calldata (function identifier)


· msg.value: (uint) The wei value sent by the current transaction


· Now: (uint) Timestamp of the current block


conclusion


In lecture 4 we looked at the scope of reference types, data locations and variables in Solidity. The key words are storage, memory and calldata. The reason for their presence is to save limited storage space on the chain and reduce gas. Next time we'll look at arrays in reference types.


The original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
This platform has fully integrated the Farcaster protocol. If you have a Farcaster account, you canLogin to comment
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit