简体   繁体   English

为什么 memory 关键字在视图 function 中被提及

[英]Why memory keyword is mentioned in view function in solidity

  function getAllWaves() public view returns (Wave[] memory) {
        return waves;
    }

I was going through _buildspace tutorial and found this code.我正在阅读 _buildspace 教程并找到了这段代码。 I had previously read that memeroy keyword is used in solidity to specify that variable should be stored in memeroy(instead of state), so that we can destroy it after function call.我之前读过 memeroy 关键字用于solidity 来指定变量应该存储在 memeroy 中(而不是状态),以便我们可以在 function 调用后销毁它。 But why memory is mentioned in return part of this view function since it just returns a value.但是为什么在此视图 function 的返回部分中提到了 memory,因为它只是返回一个值。

I am newbie in solidity so any articles relating to this also will help.我是新手,所以任何与此相关的文章也会有所帮助。 Thanks谢谢

All reference type variables (including an array) need to have their data location specified.所有引用类型变量(包括数组)都需要指定它们的数据位置

The EVM is not able to return directly from storage, as the storage location modifier acts like a pointer, and it would be useless to return just a pointer instead of the actual value. EVM 不能直接从存储中返回,因为storage位置修饰符就像一个指针,只返回一个指针而不是实际值是没有用的。

So it loads the value of waves array from storage to memory, and then returns it from memory.所以它将waves数组的值从存储加载到memory,然后从memory返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM