Appearance
数据位置 
在 Solidity 中,变量可以在三个不同的数据位置中声明:storage、memory 和 calldata。
- Storage表示变量在区块链上永久存储的位置,通常用于状态变量。
- Memory表示存储在内存中的临时变量,通常用于函数参数和局部变量。
- Calldata是一个特殊的区块链存储区域,用于存储函数调用数据,通常用于外部函数调用。
示例 
以下是一个编程示例,演示 Solidity 中不同数据位置的使用:
solidity
pragma solidity ^0.8.0;
contract DataLocation {
  uint[] myArray;
  function addToStorage(uint value) public {
    myArray.push(value);  // 存储数据到 storage
  }
  function getFromMemory(uint index) public view returns (uint) {
    uint[] memory newArray = myArray;  // 复制 storage 数据到 memory
    return newArray[index];
  }
  function getFromCalldata(uint[] calldata data) public pure returns (uint) {
    uint sum;
    for (uint i = 0; i < data.length; i++) {
      sum += data[i];  // 读取 calldata
    }
    return sum;
  }
}在上面的示例中,addToStorage 函数将数据存储在 myArray 中,该数组是一个状态变量,存储在 Storage 中。
getFromMemory 函数将 myArray 中的数据复制到一个临时数组中,该数组存储在 Memory 中。
getFromCalldata 函数接受一个包含数据的 calldata 参数,并计算其总和,calldata 存储在 calldata 区域中。
需要注意的是,在 Solidity 中,从 Storage 复制值到 Memory 是一种昂贵的操作,因此需要格外小心地使用,并尽可能减少这种操作。