涛子 - 简单就是美

成单纯魁增,永继振国兴,克复宗清政,广开家必升

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  428 随笔 :: 0 文章 :: 19 评论 :: 22万 阅读

引用类型(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;
    }
}
posted on   北京涛子  阅读(155)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示