solidity智能合约[40]-memory属性

memory引入

函数中结构体变量默认是是storage类型

下面是一段错误的代码,错误的原因在于,init函数中,student s 默认会加上storage的属性,但是storage属性必须要引用storage空间中的状态变量。但是实例化的student(100,“jackson”)并不在storage中。

1
2
3
4
5
6
7
8
9
struct student{
    uint grade;
    string name;
}

function init() public pure returns(uint,string){
    student  s = student(100,"jackson");
    return (s.grade,s.name);
}

因此,正确的做法是,必须要变量的初始化放在memory空间中。加上了memory属性的变量,意味着变量存储在memory的空间中。

1
2
3
4
5
6
7
8
9
struct student{
    uint grade;
    string name;
}

function init() public pure returns(uint,string){
    student memory s = student(100,"jackson");
    return (s.grade,s.name);
}
  • 本文链接: https://dreamerjonson.com/2018/11/23/solidity-40-memory/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

solidity智能合约[40]-memory属性