Solidity Tutorial Series 4th-Solidity data location analysis. It 's written in front .
Solidity is an intelligent contract programming language for Ethernet, you should have a better understanding of Ethernet and intelligent contracts before reading this article,
If you don't understand it, I suggest you look at the etheric square first.
This part of the content of the official English document is not very thorough, so I refer to solidity Official document (current version: 0.4.20) at the same time joined the in-depth analysis of the section, Welcome to subscribe to the column. Data Location (location)
In the first installment of the series, we mentioned that solidity types fall into two categories:
value type and reference type (Reference Types),
We've already covered the value type before, and then we'll introduce the reference type.
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 to store them, whether it is memory(in memory, the data is not permanent) or storage (Permanently stored in the block chain)
All complex types such as arrays (arrays) and data structures (struct) have an additional attribute: The storage location of the data (information location). may be 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.
function arguments (containing the returned arguments) are memoryby default.
Locally complex type variables (local variables) and state variables are storageby default.
Local variable: A variable, such as a variable within a function, that is a local scope that is unreachable and waits to be reclaimed. State variables: Public variables declared within the contract
There is also a storage location:calldata, which is used to store function arguments, is read-only, and does not permanently store a data location. The parameters of the external function (excluding the return parameter) are coerced to be specified as Calldata. The effect is similar to memory.
The data location designation is important because they affect the assignment behavior.
A completely independent copy is always created between memory and storage or with the state variables.
A storage state variable is assigned to a storage local variable, which is passed by reference. So for local variables to modify, the associated state variables are also modified.
On the other hand, assigning a memory reference type to a reference to another memory does not create a copy (that is, a reference pass between memory). Note: You cannot assign a memory to a local variable. For value types, copies are always made.
Let's look at a piece of code:
pragma solidity ^0.4.0;
The storage location of contract C {
uint[] x;// x is storage
//Memoryarray storage location is memory
function f (uint[) Memoryarray) public {
x = Memoryarray; Copy from memory to storage
var y = x; Storage reference passes local variable y (y is a storage reference)
y[7]; Returns the 8th element
y.length = 2; X will also be modified to
delete x; Y will also be modified
//error, cannot assign memory to local variable
//y = Memoryarray;
Error, cannot destroy storage
//delete y by reference;
g (x); Reference passing, G can change the contents of x
h (x); Copy to memory, H cannot change the contents of x
}
function g (uint[] storage Storagearray) Internal {}
function h (uint[) Memoryarray) Public {}
}
Summary
forced data location (forced-location)The parameters of the external function (External function) (excluding the return parameter) are enforced as follows: Calldata state variable (states variables) is forced to: storage
default data location (location)function arguments and return parameters: Memory local variables of 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 content can be changed by (transaction) calls.
Solidity called this state change, which is why contract-level variables are called state variables . You can also better understand why state variables are storage stored.
Memory can only be used inside functions, memory declarations are used to tell EVM to create a (fixed-size) area of memory at run time to use for variables.
Storage are stored in the block chain in key/value form, while memory is represented as byte arrays about stacks (stack)
EVM is a stack based language, the stack is actually in memory (memory) a data structure, each stack element accounted for 256 bits, the stack maximum length of 1024.
Local variables of value types are stored on the stack. the consumption of different storage (gas consumption) storage will permanently save the contract state variables, the most expensive memory only save temporary variables, function calls released after a small stack save small local variables, almost free of charge, but there are quantitative restrictions. reference materials
Solidity official documents-types
Simple block chain-system learning block chain, to create the best block chain technology Blog