“记忆”有什么区别。并“存储”关键词

发布于 2025-02-11 19:05:28 字数 501 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后咖啡店 2025-02-18 19:05:28

所有状态变量均已永久存储在存储中。就像硬盘存储一样。

记忆就像RAM。当合同完成其代码执行时,将清除内存。

有时,在声明状态变量后,您需要在功能中修改它。例如,您定义了

 Zombie[] public zombies;

function createZombie (string memory _name, uint _dna) public {
        Zombie storage firstZombie=zombies[0]
        // mutate the name of the firstZombie
        firstZombie.name=_name
        // you have actually mutated state variable zonbies
    }

如果使用内存关键字,则实际上将将值复制到内存:

function createZombie (string memory _name, uint _dna) public {
            // firstZombie is copied to the memory
            // New memory is allocated.
            Zombie memory firstZombie=zombies[0]
            // mutate the name of the firstZombie
            firstZombie.name=_name
            return firstZombie
            // this did not mutate the state variable zombies
            // after returning allocated memory is cleared
        }

以固体性,函数参数变量存储在内存中。

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

 Zombie[] public zombies;

function createZombie (string memory _name, uint _dna) public {
        Zombie storage firstZombie=zombies[0]
        // mutate the name of the firstZombie
        firstZombie.name=_name
        // you have actually mutated state variable zonbies
    }

If you used memory keyword, you would be actually copying the value to the memory:

function createZombie (string memory _name, uint _dna) public {
            // firstZombie is copied to the memory
            // New memory is allocated.
            Zombie memory firstZombie=zombies[0]
            // mutate the name of the firstZombie
            firstZombie.name=_name
            return firstZombie
            // this did not mutate the state variable zombies
            // after returning allocated memory is cleared
        }

In solidity, function parameter variables are stored in memory.

无需解释 2025-02-18 19:05:28

在区块链中存储存储数据,并且保持不变。记忆存储临时变量,在执行此功能时,功能中包含(在这种情况下为_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文