Notes on dynamic array assignment of solidity reference type

Throwing a question, the input parameter of the external function is a dynamic array, so it must be a memory storage type, so what happens when it is assigned to a state variable, and what happens when it is assigned to other memory dynamic arrays. The following code-friendly introduction

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.0;
import "hardhat/console.sol";

contract TestMemory{
    uint[] public data;

    function option(uint[] memory mArray) public{
        // memory->storage 属于值拷贝,storage->memory 也属于值拷贝
        data=mArray;
        // 同存储类型变量赋值,属于引用,但是此时叫引用指针,和指向的变量有所不同,区别在于引用指针不能再被重新整个赋值
        uint[] storage y =data;
        // 不安全的写法,会报警
        uint y3=y[2];
        //memory 引用类型不允许通过改方式改变长度,但是storge的可以,但是0.8等高版本后,一律不再允许
        //y.length=2;
        delete data;
        //引用指针不能再被重新整个赋值
        //y=mArray;
        data=mArray;
        // 指针变量不允许操作delete
        //delete y;
    


    }
}

Guess you like

Origin blog.csdn.net/m0_37298500/article/details/126983245