Different Types Of Memory in Solidity

Storage
Note Storage Variables are called as State Variables
In Solidity, "storage" refers to a location on the Ethereum blockchain where data is permanently stored. When a variable is declared as a state variable in a contract, it is stored in the contract's storage.
State variables are persistent and can be accessed and modified by different functions within the contract, and their values are stored permanently on the blockchain even after the contract is terminated.
Storage is typically used for storing important and persistent data in a smart contract, such as user balances, contract settings, and other critical information.
However, modifying storage variables can be expensive in terms of gas costs, as each modification requires writing data to the blockchain, which incurs gas costs for storage and computation.
Therefore, it's important to carefully consider the usage of storage variables in a smart contract and optimize gas costs whenever possible.
Here's an example of a state variable stored in storage in a Solidity contract:
contract StorageExample { uint256 public storedValue; // State variable stored in
Memory
In Solidity, the "memory" keyword is used to declare a temporary storage area that exists only during the execution of a function or a method. Memory is used for storing data that is not meant to be persisted on the blockchain and is typically used for local variables within functions or methods.
Memory is a type of storage in Solidity that is allocated at runtime and deallocated when the function or method finishes executing.
It is used for storing temporary data that is required during the execution of a function or a method but does not need to be persisted on the blockchain.
Memory is used for storing data types such as arrays, strings, structs, and mappings that are dynamically sized and may change during the execution of a function.
It is also used for returning values from functions that have dynamically sized return types.
Here's an example of using memory in Solidity:
contract MemoryExample {
function concatenateStrings(string memory _str1, string memory _str2) public pure returns (string memory) {
bytes memory str1 = bytes(_str1);
bytes memory str2 = bytes(_str2);
bytes memory result = new bytes(str1.length + str2.length);
for (uint256 i = 0; i < str1.length; i++) {
result[i] = str1[i];
}
for (uint256 j = 0; j < str2.length; j++) {
result[str1.length + j] = str2[j];
}
return string(result);
}
}
In the above example, two string parameters
_str1and_str2are passed to a function that concatenates them and returns the concatenated string.The
bytesdata type is used to perform the concatenation operation in memory, and thememorykeyword is used to specify that the data is stored in memory and not persisted on the blockchain. Once the function execution is complete, the memory allocated forresultis deallocated.
Calldata
Call data is only valid for parameters of external contract functions. Calldata is a non-modifiable, non-persistent area where function arguments are stored.
It behaves mostly like memory.
Any variable defined as calldata cannot be modifiable. In simple terms this means that you cannot change the value of the state of that variable.
This is why this data type is assigned to parameters of functions.
The data passed into the function is not modified but will be used within the function and a new variable will be returned.
In Solidity,
calldatarefers to a special area of memory that holds the input data of a contract function call. When a contract function is invoked from an external contract or from an externally owned account (EOA) via a transaction, the input data of that function call is stored in thecalldataarea of memory.The
calldatais read-only, meaning that the contract function can only read from it and cannot modify its contents. It is also limited in size, as it depends on the gas limit of the transaction.In Solidity, you can access the
calldatausing thecalldatakeyword. You can use it in function parameters or by explicitly casting it to other data types, such asbytesorbytes32, to manipulate and extract data from the input data of the function call.Here's an example:
contract CalldataExample { function processInputData(bytes calldata _inputData) public pure returns (uint256) { // Do something with the input data // e.g. return the length of the input data return _inputData.length; } }In the above example, the function
processInputDatatakes abytes calldataparameter_inputDatathat represents the input data of the function call.The function can then perform operations on
_inputData, such as accessing itslengthproperty, to process the input data. Note that thecalldatais read-only, so any attempt to modify its contents will result in a compilation error.




