Important features of solidity smart contracts

Some important features of solidity smart contracts, such as the storage and memory variables in variable-length arrays, the storage locations of storage and memory, and the use of string array string[].

More blockchain technology and application classification:

Blockchain applicationBlockchain     development

Ethernet Square | Fabric | BCOS | cryptography | consensus algorithm | bitcoinOther chain

Token EconomyTraditional Financial Scenarios | Decentralized Finance | Anti-counterfeiting Traceability | Data Sharing | Trusted Deposit

1. Regarding variable length arrays.

string[],bytes[]

For variable-length arrays, they cannot be used before initializing the allocated space. You can initialize an array with the new keyword. They cannot be accessed using subscripts before initializing with the new keyword.

storage

The storage type dynamic array has a length attribute, which represents the current array length. For storage variable-length arrays, the length of the array can be adjusted by assigning a value to length. You can change the length of the array in two ways, index++ and push method.

contract test{
    
    string[] strs = new string[](0);
    
    function test1() public returns(uint, string[]){
        strs[strs.length++] = "a1";
        return (strs.length, strs);
    }
    
    function test2() public returns(uint, string[]){
        strs.push("b1");
        return (strs.length, strs);
    }
}

memory

The dynamic array of memory type does not support modifying the length property to adjust the size of the array. Once new is over, the size cannot be changed.

2. Storage location storage and memory

Assign memory to storage

contract test1{
    int i;
    struct S{string a;uint b;}
 
    function assign(S s) internal{
        S tmp = s;  // 报错,tmp是局部变量(storage类型),s是函数参数(memory类型)
    }
    function change(int changei) public returns(int){
        i = changei;
        return i; // 返回等于changei的值
    }
}

Convert storage to storage

contract test2{
    
    struct S{string a;uint b;}
    
    //状态变量,合约内声明的公有变量,默认是storage的
    S s;
    
    function convertStorage(S storage s) internal{
        S tmp = s;  // tmp也是storage类型的
        tmp.a = "Test";
    }
    
    function call() returns (string){
        convertStorage(s);
        return s.a;//Test
    }
}

Assign memory to storage

contract test3{
  struct S{string a;uint b;}
 
  //默认是storage的
  S s;
 
  function memoryToState(S memory tmp) internal{
    s = tmp; //tmp是memory的,从内存中复制到状态变量中。
 
    //修改旧memory中的值,并不会影响状态变量
    tmp.a = "Test";
  }
 
  function call() returns(string){
    S memory tmp = S("memory", 0);
    memoryToState(tmp);
 
    return s.a;
  }
}

Assign memory to local variables (storage)

contract test4{
  struct S{string a;uint b;}
 
  function memoryToLocal(S s) internal{
    S tmp = s;  // 报错,tmp是局部变量(storage类型),s是函数参数(memory类型)
    
    //修改变量为memory类型, 不会报错
    S memory tmp = s;
  }
}

storage assignment memory

contract test5{
    struct S{string a;uint b;}
    
    S s = S("storage", 1);
    
    function storageToMemory(S storage x) internal{
        S memory tmp = x;//由Storage拷贝到memory中
        
        //memory的修改不影响storage
        tmp.a = "Test";
    }
    
    function call() returns (string){
        storageToMemory(s);
        return s.a; //storage
    }
}

memory assignment memory

The memory is passed by reference, and data is not copied. Assigning a reference type of a memory to a reference of another memory will not create another new copy.

contract test6{
  struct S{string a;uint b;}
 
  //默认参数是memory
  function memoryToMemory(S s) internal{
    S memory tmp = s; // tmp是memory,s是memory
 
    //引用传递
    tmp.a = "other memory";
  }
 
  function call() returns (string){
    S memory mem = S("memory", 1);
    memoryToMemory(mem);
    return mem.a;//other memory
  }
}

3. String array string[]

    function getEntry(string[] fields, Entry entry) internal view returns (string[]) {
        //string[] values = new string[](fields.length); 报错,values是storage类型的,后面是memory
        for (uint i = 0; i < fields.length; i++) {
            values[i] = entry.getString(fields[i]);
        }
        return values;
    }
    function getEntry(string[] fields, Entry entry) internal view returns (string[]) {
        string[] memory values = new string[](fields.length); // 正确
        for (uint i = 0; i < fields.length; i++) {
            values[i] = entry.getString(fields[i]);
        }
        return values;
    }


Original link: Important features of solidity smart contracts 

Guess you like

Origin blog.csdn.net/JonasErosonAtsea/article/details/109236301