Copyright © 2022-2025 aizws.net · 网站版本: v1.2.6·内部版本: v1.25.2·
页面加载耗时 0.00 毫秒·物理内存 146.5MB ·虚拟内存 1437.8MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
状态变量总是存储在存储区中。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DataLocation {
// storage
uint stateVariable;
uint[] stateArray;
}
此外,不能显式地标记状态变量的位置。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DataLocation {
uint storage stateVariable; // 错误
uint[] memory stateArray; // 错误
}
函数参数包括返回参数都存储在内存中。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DataLocation {
// storage
uint stateVariable;
uint[] stateArray;
function calculate(uint num1, uint num2) public pure returns (uint result) {
return num1 + num2;
}
}
此处,函数参数 uint num1 与 uint num2,返回值 uint result 都存储在内存中。
值类型的局部变量存储在内存中。但是,对于引用类型的局部变量,需要显式地指定数据位置。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Locations {
/* 此处都是状态变量 */
// 存储在storage中
bool flag;
uint number;
address account;
function doSomething() public pure {
/* 此处都是局部变量 */
// 值类型
// 所以它们被存储在内存中
bool flag2;
uint number2;
address account2;
// 引用类型,需要显示指定数据位置,此处指定为内存
uint[] memory localArray;
}
}
不能显式覆盖具有值类型的局部变量。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
function doSomething() public {
/* 此处都是局部变量 */
// 值类型
bool memory flag2; // 错误
uint Storage number2; // 错误
address account2;
}
外部函数的参数(不包括返回参数)存储在 calldata中。
solidity 数据可以通过两种方式从一个变量复制到另一个变量。一种方法是复制整个数据(按值复制),另一种方法是引用复制。solidity 从一个位置的变量赋值到另一个位置的变量,遵循一定的默认规则。 ...