Storage and memory data locations
Each variable declared and used within a contract data location. EVM provides four data structures for storing variables:
Storage: global memory available functions within the contract. This storage is permanent storage that Ethereum stores on every node within its environment.
Memory: local memory available function within a contract. a short-lived and fleeting memory that gets torn down when the function completes its execution.
Calldata: where all incoming function execution data, including function arguments, is stored. a non-modifiable memory location.
Stack: EVM maintains a stack for loading variables and intermediate values for working with the Ethereum instruction set. working set memory for EVM. A stack is 1,024 levels deep in EVM and if it stores anything this it raises an exception. location of a variable on two factors:
- Location of variable declaration
- The data the variable
Based on the preceding two factors, there are rules that govern location of a variable. are mentioned here. Data locations also affect the way the assignment operator works. Both assignment and data locations are explained by means of rules that govern them.
Variables declared as state variables are always stored storage data location.
Variables declared as function parameters are always stored memory data location.
Variables declared within functions, by default, are stored in memory data location.
However, there are caveats:
The location for value type variables is memory within a function while the default for a reference type variable is storage. Please note that storage default for reference type variables declared within a function. However, it overridden. By overriding the default location, reference types variables located at the memory data location. The reference types referred are arrays, structs, and strings. Reference types declared within a function without being overridden point to a state variable. Value type variables declared function overridden be stored at the storage location. Mappings are always declared at the storage location. that be declared within a function. be declared as memory types. However, mappings function can mappings declared as state variables.
Arguments supplied by callers to function parameters are always stored call data data
Assignments to state variables from another state variable always create copy. Two value type state variables stateVar1 and stateVar2 are declared.
Assignments to storage variables from another memory variable always create copy. array of uint stateArray as a state variable. Within the getUInt function, memory located fixed array of uint localArray is defined and initialized. the line of code assigns localArray to stateArray.
Assignments to memory variable from another state variable always create copy. type state variable, stateVar and initialized with value 20. Within the getUInt function, variable of type uint and initiated with value 40.
The stateVar variable is assigned to the localVar variable. At this stage, the values in both variables are 20. the line of code changes of stateVar to 50 and returns localVar .
Assignments to a memory variable from another memory variable create for reference types; however, create copy for value types.
The code listing shown in the following screenshot illustrates that reference type variables in memory are copied by reference. The value of otherVar is affected by changes in the someVar variable: