Data Locations in Solidity EVM
The storage is the permanent storage of data. Store data are written to the blockchain and are available for everyone. It is the most expensive type of storage. These variables are also called state variables.
Typically, contract variables and local variables of struct, mapping or array type are stored in the storage.
Memory is an EVM internal storage. After the execution of a function, the memory is deleted. Compared to the storage, it is relatively cheap. Typically function arguments and variables defined inside a function are stored in memory.
It is possible to force the EVM to store structs, mappings, and arrays in the memory. All other values like uint or bool that are created within a function are stored in memory by default.
struct User{
uint256 id;
string name;
}
function myFunction() public{
User memory user;
}
First, we define a struct, and in the function, we initialize it in memory with the keyword memory. It is also possible to store it in storage by using the keyword storage instead.
The stack is also an EVM internal storage. It has a limited size and holds local variables of value type (uint, int, etc.).
Calldata behaves like memory. It is an immutable and non-persistent (temporary) location where function arguments are stored.