“记忆”有什么区别。并“存储”关键词
pragma solidity >=0.5.0 <0.6.0;
contract ZombieFactory {
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
function createZombie (string memory _name, uint _dna) public {
// start here
}
}
在这里,我感到困惑,因为根据这篇文章
= 743A8DDB20C449DF924652051C14EF26 函数参数始终在内存中“ ”。 因此,这是否意味着在此代码中,当我们将字符串_name作为函数参数传递时,它将被分配给内存,还是像所有其他状态变量一样保留在存储中?
pragma solidity >=0.5.0 <0.6.0;
contract ZombieFactory {
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
function createZombie (string memory _name, uint _dna) public {
// start here
}
}
Here I am confused because as per this post https://ethereum.stackexchange.com/questions/1701/what-does-the-keyword-memory-do-exactly?newreg=743a8ddb20c449df924652051c14ef26
"the local variables of struct are by-default in storage, but the function arguments are always in memory".
So does it mean that in this code when we pass string _name as a function argument, it will be assigned to memory or will it remain in the storage like all other state variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有状态变量均已永久存储在
存储
中。就像硬盘存储一样。记忆就像RAM。当合同完成其代码执行时,将清除内存。
有时,在声明状态变量后,您需要在功能中修改它。例如,您定义了
如果使用
内存
关键字,则实际上将将值复制到内存:以固体性,函数参数变量存储在内存中。
All the state variables are stored in
storage
permanently. It is like hard disk storage.Memory is like RAM. When a contract finishes its code execution, the memory is cleared.
Sometimes after you declared a state variable, you want to modify it inside a function. For example you defined
If you used
memory
keyword, you would be actually copying the value to the memory:In solidity, function parameter variables are stored in memory.
在区块链中存储存储数据,并且保持不变。记忆存储临时变量,在执行此功能时,功能中包含(在这种情况下为_name)及其寿命有限。因此,当我们将字符串_name作为函数参数传递时,它将被分配给内存,而您在struct(name)中的变量将被调用到存储中。
Storage store data in blockchain and it remains immutable. Memory store temporary variables, that included in functions(in this case _name) and their lifetime limited while executing this function. So when we pass string _name as a function argument, it will be assigned to memory, while your variable in struct(name) will be assingned to storage.