Skip to main content

Command Palette

Search for a command to run...

Different Types Of Memory in Solidity

Published
4 min readView as Markdown
Different Types Of Memory in Solidity

Storage

  1. Note Storage Variables are called as State Variables

  2. 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.

  3. 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.

  4. Storage is typically used for storing important and persistent data in a smart contract, such as user balances, contract settings, and other critical information.

  5. 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.

  6. 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

  1. 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.

  2. Memory is a type of storage in Solidity that is allocated at runtime and deallocated when the function or method finishes executing.

  3. 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.

  4. 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.

  5. 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);
    }
}
  1. In the above example, two string parameters _str1 and _str2 are passed to a function that concatenates them and returns the concatenated string.

  2. The bytes data type is used to perform the concatenation operation in memory, and the memory keyword 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 for result is deallocated.

Calldata

  1. Call data is only valid for parameters of external contract functions. Calldata is a non-modifiable, non-persistent area where function arguments are stored.

  2. It behaves mostly like memory.

  3. 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.

  4. This is why this data type is assigned to parameters of functions.

  5. The data passed into the function is not modified but will be used within the function and a new variable will be returned.

  6. In Solidity, calldata refers 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 the calldata area of memory.

  7. The calldata is 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.

  8. In Solidity, you can access the calldata using the calldata keyword. You can use it in function parameters or by explicitly casting it to other data types, such as bytes or bytes32, 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;
         }
     }
    
  9. In the above example, the function processInputData takes a bytes calldata parameter _inputData that represents the input data of the function call.

  10. The function can then perform operations on _inputData, such as accessing its length property, to process the input data. Note that the calldata is read-only, so any attempt to modify its contents will result in a compilation error.

More from this blog