Smart Contract Language Solidity Tutorial Series 4-Data storage location analysis

Source: Internet
Author: User

Write in front

Solidity is the Ethereum Smart Contract programming language, you should know about Ethereum and smart contracts before reading this article.
If you don't understand, it's recommended that you look at Ethereum first.

This part of the official English document is not very thorough, so I refer to the solidity Official document (currently the latest version: 0.4.20) In addition to the in-depth analysis section.

Data location

In the first series, we mentioned that solidity types fall into two categories:
value type and reference type (Reference Types),
We've covered the value types earlier, and we'll cover the reference types.

A reference type is a complex type that takes up more than 256 bits of space and is expensive to copy, so we need to consider where we store it, whether it is memory (the data does not exist permanently) or storage (Permanently stored in the blockchain)
All complex types such as array (arrays) and struct (struct) have an additional attribute: The data location where thedatastore is stored. Available for memory and storage.

Depending on the context, most of the time the data location has a default value, and it is modified by specifying the keyword storage and memory.

The function parameter (which contains the returned parameter) is memoryby default.
The local complex type variable (local variables) and the state variable (variables) are storageby default.

Local variables: variables, such as variables within a function, that are local scopes (which are not accessible, waiting to be reclaimed), beyond scope. State variables: Public variables declared within a contract

There is also a storage location:calldata, which is used to store function parameters, is read-only and does not permanently store a data location. Parameters of external functions (not including return parameters) are forced to be specified as Calldata. The effect is almost the same as memory.

Data location designations are important because they affect the assignment behavior.
Assigning values between memory and storage or with state variables always creates a completely separate copy.
Instead, a storage state variable is assigned to a storage local variable that is passed by reference. So for the modification of local variables, the associated state variables are also modified.
On the other hand, assigning a memory reference type to another memory reference does not create a copy (that is, a reference pass between memory).

  1. Note: You cannot assign a memory to a local variable.
  2. For value types, copies are always made.

Here's a look at the code:

pragma solidity ^0.4.0;contract C {    uint[] x; //  x的存储位置是storage    // memoryArray的存储位置是 memory    function f(uint[] memoryArray) public {        x = memoryArray;    // 从 memory 复制到 storage        var y = x;          // storage 引用传递局部变量y(y 是一个 storage 引用)        y[7];               // 返回第8个元素        y.length = 2;       // x同样会被修改        delete x;           // y同样会被修改        // 错误, 不能将memory赋值给局部变量        // y = memoryArray;          // 错误,不能通过引用销毁storage        // delete y;                g(x);               // 引用传递, g可以改变x的内容        h(x);               // 拷贝到memory, h无法改变x的内容    }    function g(uint[] storage storageArray) internal {}    function h(uint[] memoryArray) public {}}
Summarize the mandatory data location (forced)
    • arguments to external functions (External function) (not including return parameters) are forced to: calldata
    • The state variables is coerced to: storage
Default data location
    • function parameters and return parameters: Memory
    • Local variables for complex types: storage
In-depth analysis

The storage storage structure is determined when the contract is created, depending on the state variable declared by the contract. But the content can be changed by the (trade) call.

Solidity called this a state change, which is why contract-level variables are called state variables . It is also better to understand why state variables are storage stored.

Memory can only be used inside the function, and memory declarations are used to tell EVM to create a piece (fixed size) memory area at run time for variable use.

Storage is stored as key/value in the blockchain, while memory is represented as a byte array

About Stacks (stack)

EVM is a stack-based language, and the stack is actually a data structure in memory, with each stack element occupying 256 bits and a maximum stack length of 1024.
Local variables of value types are stored on the stack.

Consumption of different storage (gas consumption)
    • Storage will permanently save the contract state variable, the most expensive
    • Memory only saves temporary variables, and is freed after function calls, with minimal overhead
    • The stack holds small local variables that are almost free to use, but with a limited number.
Resources

Solidity official documents-type

In-depth blockchain-the system learns blockchain to create the best blockchain technology blog.

Smart Contract Language Solidity Tutorial Series 4-Data storage location analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.