引用类型(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;
}
}

solidity语言4的更多相关文章

  1. 用solidity语言开发代币智能合约

    智能合约开发是以太坊编程的核心之一,而代币是区块链应用的关键环节,下面我们来用solidity语言开发一个代币合约的实例,希望对大家有帮助. 以太坊的应用被称为去中心化应用(DApp),DApp的开发 ...

  2. 第一行代码:以太坊(2)-使用Solidity语言开发和测试智能合约

    智能合约是以太坊的核心之一,用户可以利用智能合约实现更灵活的代币以及其他DApp.不过在深入讲解如何开发智能合约之前,需要先介绍一下以太坊中用于开发智能合约的Solidity语言,以及相关的开发和测试 ...

  3. Solidity语言系列教程

    Solidity 是一门面向合约的.为实现智能合约而创建的高级编程语言.这门语言受到了 C++,Python 和 Javascript 语言的影响,设计的目的是能在 以太坊虚拟机(EVM) 上运行. ...

  4. solidity语言介绍以及开发环境准备

    solidity语言介绍以及开发环境准备   Solidity 是一门面向合约的.为实现智能合约而创建的高级编程语言.这门语言受到了 C++,Python 和 Javascript 语言的影响,设计的 ...

  5. 用C++生成solidity语言描述的buchi自动机的初级经验

    我的项目rvtool(https://github.com/Zeraka/rvtool)中增加了生成solidity语言格式的监控器的模块. solidity特殊之处在于,它是运行在以太坊虚拟机环境中 ...

  6. Solidity语言基础 和 Etherum ERC20合约基础

    1. 类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的,  小. for (var i = 0; i < 2000; i++) { ... } --- Solidity Types ...

  7. solidity语言

    IDE:Atom 插件:autocomplete-solidity 代码自动补齐   linter-solium,linter-solidity代码检查错误   language-ethereum支持 ...

  8. solidity语言14

    库(Libraries) 库类似合约,实现仅在专门地址部署一次,使用EVM的DELEGATECALL的功能重复使用的目的.意思是当库函数被调用后,代码执行在被调用的合约的环境.例如,使用this调用合 ...

  9. solidity语言13

    函数过载 合约内允许定义同名函数,但是输入参数不一致 pragma solidity ^0.4.17; contract A { function f(uint _in) public pure re ...

  10. solidity语言12

    View Functions 函数声明为视图,将无权修改状态 pragma solidity ^0.4.16; contract C { function f(uint a, uint b) publ ...

随机推荐

  1. 洛谷 P1272 重建道路

    题目链接 题解 树形dp \(f_{i, j}\)表示以\(i\)为根的子树切出联通块大小为\(j\)的最小答案 显然\(f[i][1]\)为与\(i\)连的边数 设\(v\)是\(u\)的儿子 那么 ...

  2. 关于XML的验证(DTD与XSD)一点实践

    [转自] http://blog.chinaunix.net/uid-276853-id-366491.html 关于XML的验证一点实践 1)此方法是在XML文档中绑定对应的DTD文件来进行的 // ...

  3. 尺取法 javascript算法

    给定长度为n的数列整数a0,a1……an-1 以及整数S.求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. 输入 n=10 S=15 a=[5,1,3,5,10,7,4,9,2,8 ...

  4. naginx安装入门

    一.nginx是什么 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件.它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用. nginx比它大哥apach ...

  5. css3 渐变色

    Firefox可以使用角度来设定渐变的方向,而webkit只能使用x和y轴的坐标. 渐变可以创建类似于彩虹的效果,低版本的浏览器使用图片来实现,CSS3将会轻松实现网页渐变效果 粘贴代码 <di ...

  6. [jQuery] 在线引用地址

    百度静态资源公共库: http://libs.baidu.com/jquery/1.9.1/jquery.js jQuery网站: http://code.jquery.com/jquery-1.9. ...

  7. css 浮动元素居中

    方法一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  8. (转)Linux系统sersync数据实时同步

    Linux系统sersync数据实时同步 原文:http://blog.csdn.net/mingongge/article/details/52985259 前面介绍了以守护进程的方式传输或同步数据 ...

  9. (转)Unity 之 UGUI 小总结

    转自:http://www.jianshu.com/p/5b6f5022662e 开发过程中对UGUI的一个小总结. 首先从原画师拿到效果图,美术切图,拿到碎图后打成大图. 我们先来说一下图:RGBA ...

  10. 32位x86处理器编程导入——《x86汇编语言:从实模式到保护模式》读书笔记08

    在说正题之前,我们先看2个概念. 1.指令集架构(ISA) ISA 的全称是 instruction set architecture,中文就是指令集架构,是指对程序员实际"可见" ...