Storage and memory data locations
Each variable declared and used within a contract features a data location. EVM provides the subsequent four data structures for storing variables:
Storage: This is often global memory available to all or any functions within the contract. This storage is permanent storage that Ethereum stores on every node within its environment.
Memory: this is often local memory available to each function within a contract. this is often a short-lived and fleeting memory that gets torn down when the function completes its execution.
Calldata: this is often where all incoming function execution data, including function arguments, is stored. this is often a non-modifiable memory location.
Stack: EVM maintains a stack for loading variables and intermediate values for working with the Ethereum instruction set. this is often working set memory for EVM. A stack is 1,024 levels deep in EVM and if it stores anything quite this it raises an exception. the info location of a variable depends on the subsequent two factors:
Location of variable declaration
The data sort of the variable
Based on the preceding two factors, there are rules that govern and choose the info location of a variable. the principles 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 within the storage data location.
Variables declared as function parameters are always stored within the memory data location.
Variables declared within functions, by default, are stored in memory data location.
However, there are next few 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 is that the default for reference type variables declared within a function. However, it is often overridden. By overriding the default location, reference types variables are often located at the memory data location. The reference types referred are arrays, structs, and strings. Reference types declared within a function without being overridden should point to a state variable. Value type variables declared during a function can’t be overridden and can’t be stored at the storage location. Mappings are always declared at the storage location. this suggests that they can’t be declared within a function. they can’t be declared as memory types. However, mappings during a function can ask mappings declared as state variables.
Arguments supplied by callers to function parameters are always stored during a call data data
Assignments to state variables from another state variable always create a replacement copy. Two value type state variables stateVar1 and stateVar2 are declared.
Assignments to storage variables from another memory variable always create a replacement copy. a hard and fast array of uint stateArray is said as a state variable. Within the getUInt function, an area memory located fixed array of uint localArray is defined and initialized. the subsequent line of code assigns localArray to stateArray . At this stage, the values in both the variables are an equivalent. the subsequent line of code changes one among the values in the local array to 10 and returns the element at an equivalent location from the stateArray1 array.
Assignments to memory variable from another state variable always creates a replacement copy. a worth type state variable, stateVar is said and initialized with value 20 . Within the getUInt function an area variable of type uint is said and initiated with value 40 . The stateVar variable is assigned to the localVar variable. At this stage, the values in both the variables are 20 . subsequent line of code changes the worth of stateVar to 50 and returns localVar .
Assignments to a memory variable from another memory variable don’t create a replica for reference types; however, they are doing create a replacement copy for value types.
For more details visit: https://www.technologiesinindustry4.com/how-to-store-and-locate-memory-data-in-solidity/