solidity语言4

引用类型(Reference Types)

memory 不支持持久保存
storage 保留为变量

复杂类型如arrays和structs,有附加信息,‘data location’,提示存储在'memory'或者'storage'。函数参数默认使用memory,本地变量默认使用storage.
pragma solidity ^0.4.17;

contract C {
    uint[] x;  // 存储在storage

    function f(uint[] memoryArray) public { // memoryArray存储在memory
        x = memoryArray;   // 复制memoryArray到storage
        var y = x; // y 存储在storage
        y[7];            //  返回第8字符
        y.length = 2;        // 通过y修改x
        delete x;       // 清除数组,同时修改y
        g(x);  // 调用g,处理到x的引用
        h(x); // 调用h,建立依赖,临时复制到memory
    }

    function g(uint[] storage storageArray) internal {}
    function h(uint[] memoryArray) public {}
}

数组

固定长度数组 T[k]
非固定长度数组 T[]

访问方法 x[2][1]

bytes 和 string是特殊数组,string等同于bytes,但不不允许使用成员属性length和使用索引访问。

成员属性
length
push

在内存中建立数组

使用new关键字在内存中建立可变长度数组,相对于stroge中的数组,它不能使用length改变长度

pragma solidity ^0.4.16;

contract C {
    function f(uint len) public pure {
        uint[] memory a = new uint[] (7);
        bytes memory b = new bytes (len); 
        // a.length == 7, b.length == len
        a[6] = 8;
    }
}

数组常量

数组常量只能通过表达式声明,不能批派到变量

pragma solidity ^0.4.16;

contract C {
    function f() public pure {
        g([uint(1), 2, 3]);
    }

    function g(uint[3] _data) public pure {
    // ...
    }
}

以下是错误的
pragma solidity ^0.4.0;

contract C {
    function f() public {
    // The next line creates a type error because uint[3] memory cannot be converted to uint[] memory.
    uint[] x = [uint(1), 3, 4];
    }
}

完整例子

pragma solidity ^0.4.16;

contract ArrayContract {
    uint[2**20] m_aLotOfIntegers;
    // Note that the following is not a pair of dynamic arrays but a
    // dynamic array of pairs (i.e. of fixed size arrays of length two).

    bool[2][] m_pairsOfFlags;
    
    // newPairs is stored in memory - the default for function arguments
    2function setAllFlagPairs(bool[2][] newPairs) public {
        // assignment to a storage array replaces the complete array
        m_pairsOfFlags = newPairs;
    }

    function setFlagPair(uint index, bool flagA, bool flagB) public {
        // access to a non-existing index will throw an exception
        m_pairsOfFlags[index][0] = flagA;
        m_pairsOfFlags[index][1] = flagB;
    }

    function changeFlagArraySize(uint newSize) public {
        // if the new size is smaller, removed array elements will be cleared
        m_pairsOfFlags.length = newSize;
    }

    function clear() public {
        // these clear the arrays completely
        delete m_pairsOfFlags;
        delete m_aLotOfIntegers;

        // identical effect here
        m_pairsOfFlags.length = 0;
    }

    bytes m_byteData;
    function byteArrays(bytes data) public {
        // byte arrays ("bytes") are different as they are stored without padding,
        // but can be treated identical to "uint8[]"
        m_byteData = data;
        m_byteData.length += 7;
        m_byteData[3] = byte(8);
        delete m_byteData[2];
    }

    function addFlag(bool[2] flag) public returns (uint) {
        return m_pairsOfFlags.push(flag);
    }

    function createMemoryArray(uint size) public pure returns (bytes) {
        // Dynamic memory arrays are created using `new`:
        uint[2][] memory arrayOfPairs = new uint[2][](size);

        // Create a dynamic byte array:
        bytes memory b = new bytes(200);
        for (uint i = 0; i < b.length; i++)
            b[i] = byte(i);
        return b;
    }
}
原文地址:https://www.cnblogs.com/liujitao79/p/8479824.html