Why do I use the "memory" keyword in Ethereum's Solidity?

Asked 2 years ago, Updated 2 years ago, 96 views

When you look at the sample smart direct provided by Ethereum, the arrays are sometimes declared "memory" in the function, and sometimes they are not. What's the difference?

Source: https://stackoverflow.com/questions/33839154/in-ethereum-solidity-what-is-the-purpose-of-the-memory-keyword This question is authorized to change the same condition (https://creativecommons.org/licenses/by-sa/3.0/deed.ko.

ethereum

2022-09-22 13:12

1 Answers

Without the memory keyword, solidity stores the variables in storage.

dev chriseth: storage is a large array of virtual structures. This structure cannot be changed at runtime and is determined by the static variables in the direct.

The structure of Storage is determined by a variable declaration at the contact level and is not changed by a function call later. However, the content can be changed by calling the sendTransaction function. These calls are called "state", which is why the variables at the contact level are called "state variables". Therefore, even if variable uint8 storagevar; is declared at the contract level, the value can change at any time within the range of uint8 (0-255), but the type uint8 cannot change.

If the function does not declare the variables as memory, solidity will try to use the storage structure by default and the value can change at any time, which can lead to unexpected results when compiled. Memory asks solidity for a variable in a function at runtime that is guaranteed the size and structure of the space when used later in the function.

Memory is not available at the direct level and is only available for functions.

See the What is the memory keyword? What does it do? tab at FAQ.

Here's a quote:

Ethereum Virtual Machine stores something in three areas when you save it.
The first is "storage" with static variables. Areas in all direct that do not change in function calls, but are expensive to use.
Second, there is a "memory" area that stores temporary values. External functions are cleared when called and are less expensive when used. Third, there is a "stack" area that you use to store regional variables. It costs little when used, but there is a limited number of storage available.
Users often decide where to save, so they cannot specify where.
Good for declaring structures and arrangements in storage areas. If you pass these structures or array variables over to the factor value of the function call, their data will not be copied if it can be in the storage or memory area. This means that you can change the internal content of the called function and see these changes on the called side as well.
There are also types of variables that are fixed to storage.
1: State variables always enter the storage area.
2: Function factors always enter the memory area.
3: Regional variables of structure, variable, and mapping type enter the storage area by default.
4: The rest will enter the stack area.


2022-09-22 13:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.