Solidity Blockchain - Memory,Storage and mapping explanation 简要说明
Solidity Blockchain - Memory,Storage and mapping explanation brief explanation
有人可以用例子简单地向我解释一下映射、存储和内存吗?有些文章不清楚
谢谢!!
存储和内存
Solidity 中的 Storage 和 Memory 关键字类似于计算机的硬盘驱动器和计算机的 RAM。与 RAM 非常相似,Solidity 中的 Memory 是一个 临时 存储数据的地方,而 Storage 在函数调用之间保存数据。 Solidity 智能合约在执行期间可以使用任意数量的内存,但一旦执行停止,内存将 完全擦除 以供下一次执行。
另一方面,Storage 是持久的,智能合约的每次执行都可以访问 以前存储在存储区域中的数据。
以太坊虚拟机上的每笔交易都会花费我们一定数量的 Gas。 Gas 消耗越低,你的 Solidity 代码就越好。 Memory 的 Gas 消耗与 Storage 的 Gas 消耗相比不是很显着。因此,中间计算最好使用Memory,将最终结果存入Storage
- 结构体的状态变量和局部变量,数组总是
默认存储在存储中。
- 函数参数在内存中。
- 每当使用关键字创建数组的新实例时
‘memory’,创建该变量的新副本。
- 改变新实例的数组值不影响
原始数组。
示例#1:在下面的示例中,创建了一个合约来演示“存储”关键字。
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising array numbers
int[] public numbers;
// Function to insert values
// in the array numbers
function Numbers() public
{
numbers.push(1);
numbers.push(2);
//Creating a new instance
int[] storage myArray = numbers;
// Adding value to the
// first index of the new Instance
myArray[0] = 0;
}
}
输出:
当我们在上面的代码中获取数组数字的值时,注意数组的输出是[0,2]而不是[1,2]。
示例#2:在下面的示例中,创建一个合约来演示关键字“内存”。
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising array numbers
int[] public numbers;
// Function to insert
// values in the array
// numbers
function Numbers() public
{
numbers.push(1);
numbers.push(2);
//creating a new instance
int[] memory myArray = numbers;
// Adding value to the first
// index of the array myArray
myArray[0] = 0;
}
}
输出:
当我们在上面的代码中获取数组numbers的值时,注意数组的输出是[1,2]。在这种情况下,更改 myArray 的值不会影响数组编号中的值,这是因为函数停止,数组未保存
映射
映射是完全不同的东西。
这些用于以键值对的形式存储数据,键可以是任何内置数据类型但不允许引用类型,而值可以是任何类型。
映射主要(但不限于)用于将唯一的以太坊地址与关联的值类型相关联。
映射与数组非常相似
mapping(key => value) <name>;
示例#1:
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising mapping of user balance
mapping(address => uint) balance;
// Function to insert user balance
function Insert(address _user, uint _amount) public
{
//insert the amount to a specific user
balance[_user] = _amount
}
//function to view the balance
function View(address _user) public view returns(uint)
{
//see the value inside the mapping, it will return the balance of _user
return balance[_user];
}
}
有人可以用例子简单地向我解释一下映射、存储和内存吗?有些文章不清楚
谢谢!!
存储和内存
Solidity 中的 Storage 和 Memory 关键字类似于计算机的硬盘驱动器和计算机的 RAM。与 RAM 非常相似,Solidity 中的 Memory 是一个 临时 存储数据的地方,而 Storage 在函数调用之间保存数据。 Solidity 智能合约在执行期间可以使用任意数量的内存,但一旦执行停止,内存将 完全擦除 以供下一次执行。 另一方面,Storage 是持久的,智能合约的每次执行都可以访问 以前存储在存储区域中的数据。
以太坊虚拟机上的每笔交易都会花费我们一定数量的 Gas。 Gas 消耗越低,你的 Solidity 代码就越好。 Memory 的 Gas 消耗与 Storage 的 Gas 消耗相比不是很显着。因此,中间计算最好使用Memory,将最终结果存入Storage
- 结构体的状态变量和局部变量,数组总是 默认存储在存储中。
- 函数参数在内存中。
- 每当使用关键字创建数组的新实例时 ‘memory’,创建该变量的新副本。
- 改变新实例的数组值不影响 原始数组。
示例#1:在下面的示例中,创建了一个合约来演示“存储”关键字。
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising array numbers
int[] public numbers;
// Function to insert values
// in the array numbers
function Numbers() public
{
numbers.push(1);
numbers.push(2);
//Creating a new instance
int[] storage myArray = numbers;
// Adding value to the
// first index of the new Instance
myArray[0] = 0;
}
}
输出:
当我们在上面的代码中获取数组数字的值时,注意数组的输出是[0,2]而不是[1,2]。
示例#2:在下面的示例中,创建一个合约来演示关键字“内存”。
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising array numbers
int[] public numbers;
// Function to insert
// values in the array
// numbers
function Numbers() public
{
numbers.push(1);
numbers.push(2);
//creating a new instance
int[] memory myArray = numbers;
// Adding value to the first
// index of the array myArray
myArray[0] = 0;
}
}
输出:
当我们在上面的代码中获取数组numbers的值时,注意数组的输出是[1,2]。在这种情况下,更改 myArray 的值不会影响数组编号中的值,这是因为函数停止,数组未保存
映射
映射是完全不同的东西。 这些用于以键值对的形式存储数据,键可以是任何内置数据类型但不允许引用类型,而值可以是任何类型。 映射主要(但不限于)用于将唯一的以太坊地址与关联的值类型相关联。 映射与数组非常相似
mapping(key => value) <name>;
示例#1:
pragma solidity ^0.4.17;
// Creating a contract
contract helloGeeks
{
// Initialising mapping of user balance
mapping(address => uint) balance;
// Function to insert user balance
function Insert(address _user, uint _amount) public
{
//insert the amount to a specific user
balance[_user] = _amount
}
//function to view the balance
function View(address _user) public view returns(uint)
{
//see the value inside the mapping, it will return the balance of _user
return balance[_user];
}
}