Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Where is the location of Solidity data storage?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "where is the location of Solidity data storage". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Data location (Data location)

In the first article of the series, we mentioned that Solidity types are divided into two categories: value types (Value Type) and reference types (Reference Types). We have already introduced value types, and then we will introduce reference types.

Reference types are complex types that usually take up more than 256bits and are expensive to copy, so we need to consider where to store them, whether memory (in memory, the data is not permanent) or storage (permanently stored in the blockchain) all complex types such as arrays (arrays) and structures (struct) have an additional attribute: the location where the data is stored (data location). Can be memory and storage.

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

Function parameters (including the returned parameters) default to memory. Local complex type variables (local variables) and state variables (state variables) default to storage.

Local variable: a variable with a local scope (beyond the scope that is inaccessible, waiting to be recycled), such as a variable within a function. State variables: public variables declared in the contract

There is also a storage location: calldata, which is used to store function parameters, a data location that is read-only and not permanently stored. The parameters of the external function (excluding the return parameters) are forced to be specified as calldata. The effect is similar to that of memory.

Data location assignments are important because they affect assignment behavior. Assigning values to each other between memory and storage or with state variables always creates a completely independent copy. And assigning a state variable of storage to a local variable of storage is passed by reference. So for the modification of the local variable, modify the associated state variable at the same time. On the other hand, assigning a reference type of one memory to a reference of another memory does not create a copy (that is, reference passing between memory).

Note: memory cannot be assigned to local variables.

For value types, it is always copied.

Let's look at a piece of code:

Pragma solidity ^ 0.4.0X contract C {uint [] x; / x is stored in storage / / memoryArray is stored in memory function f (uint [] memoryArray) public {x = memoryArray; / / copied from memory to storage var y = x; / / the storage reference passes the local variable y (y is a storage reference) y [7] / / return the 8th element y.length = 2; / / x will also be modified delete x; / / y will also be modified / / error, memory cannot be assigned to the local variable / / y = memoryArray; / / error, storage / / delete y cannot be destroyed by reference G (x); / / reference passing, g can change the content of x h (x); / / copy to memory, h cannot change the content of x} function g (uint [] storage storageArray) internal {} function h (uint [] memoryArray) public {} summary forced data location (Forced data location)

The parameters of the external function (External function) (excluding the return parameters) are forced to: calldata

The state variable (State variables) is forced to: storage

Default data location (Default data location)

Function parameters and return parameters: memory

Local variables of complex types: storage

Deep analysis

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

Solidity calls this a state change, which is why contract-level variables are called state variables. You can also better understand why state variables are stored in storage.

Memory can only be used inside a function, and the memory declaration is used to tell EVM to create a (fixed size) memory area for use by variables at run time.

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

About Stack (stack)

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

Consumption of different storage (gas consumption)

Storage keeps the contract status variable permanently, which is the most expensive.

Memory saves only temporary variables, which is released after the function call, and the overhead is very small.

Stack holds a small number of local variables, which are almost free to use, but with a limited number.

This is the end of the content of "where is the location of Solidity data storage". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report