Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 116.2MB ·虚拟内存 1372.4MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
引用类型在 Solidity 中数据有一个额外的属性:存储位置,可选项为 memory 和 storage。
外部函数 (external function) 的参数强制为:calldata。
状态变量强制为: storage。
pragma solidity ^0.8.0;
contract StateVariable{
struct S{ string a; uint b; }
//状态变量,默认是 storage
S s;
}
函数参数,返回参数:memory。
局部变量:storage。
pragma solidity ^0.8.0;
contract SimpleAssign{ struct S{ string a; uint b; } function assign(S s) internal{ // 函数参数默认是 memory 类型,即 s 是 memory 类型
// 局部变量默认 storage 类型
// S tmp = s; 报错
}
}
这里将一个memory类型的参数赋值给storage类型的局部变量会报错。
把一个storage类型赋值给一个 storage 类型时,只是修改其指针(引用传递)。
pragma solidity ^0.8.0;
contract StorageToStorageTest{
struct S{string a;uint b;}
//默认是storage的
S s;
function storageTest(S storage s) internal{
S test = s;
test.a = "Test";
}
function call() returns (string){
storageTest(s);
return s.a; //Test
}}
分为 2 种情况:
a. 将 memory–>状态变量; 即将内存中的变量拷贝到存储中(值传递)
pragma solidity ^0.8.0;
contract StorageToStorageTest{
struct S{string a;uint b;}
//默认是storage
S s;
function storageTest(S s) internal{
s = s;
s.a = "Test";
}
function call() returns (string){
storageTest(s);
return s.a;
}
}
b.将memeory–>局部变量 报错
3.storage–>memory:即将数据从storage拷贝到memory中
pragma solidity ^0.4.0;
contract StorageToMemory{
struct S{string a;uint b;}
S s = S("storage", 1);
function storageToMemory(S storage x) internal{
S memory tmp = x;//由Storage拷贝到memory中
//memory的修改不影响storage
tmp.a = "Test";
}
function call() returns (string){
storageToMemory(s);
return s.a;//storage
}
}
4.memory–>memory 和storage转storage一样是引用传递
pragma solidity ^0.4.0;
contract MemoryToMemory{
struct S{string a;uint b;}
function smemoryTest(S s) internal{
S memory test = s;
test.a = "Test";
}
function call() returns (string){
//默认是storage的
S memory s = S("memory",10);
smemoryTest(s);
return s.a;//Test
}
}
本章节主要介绍 solidity 中引用类型的属性和注意点,引用类型包括: 可变字节数组,数组,结构体。1. 可变字节数组string:是一个动态尺寸的utf-8编码字符串,他其实是一个特殊的可变字节数组,同时其 ...