In blockchain programming, especially in smart contracts, you need to know where data is kept. Storage, memory, and calldata are places to store data, each with different uses and costs.
Storage vs memory vs calldata in Blockchain / Solidity
function example(uint[] memory tempArray, uint[] calldata inputArray) public {
uint[] storage savedArray;
// Use tempArray, inputArray, and savedArray here
}storage means data is saved permanently on the blockchain.
memory means data is temporary and only exists during the function call.
calldata is a special read-only area for function inputs, cheaper than memory.
uint[] storage myStorageArray; // Permanent data stored on blockchain
function tempData() public {
uint[] memory tempArray = new uint[](5); // Temporary data
}function readOnly(uint[] calldata input) external { // input is read-only and cheaper to use }
This smart contract shows how to use storage, memory, and calldata. The saveData function takes input from calldata and saves it permanently in storage. The getTempSum function copies storage data to memory to calculate the sum without changing the stored data.
pragma solidity ^0.8.0; contract DataExample { uint[] public storedData; // storage function saveData(uint[] calldata inputData) external { // Clear old data delete storedData; // Copy inputData from calldata to storage for (uint i = 0; i < inputData.length; i++) { storedData.push(inputData[i]); } } function getTempSum() public view returns (uint) { uint[] memory tempArray = storedData; // copy storage to memory uint sum = 0; for (uint i = 0; i < tempArray.length; i++) { sum += tempArray[i]; } return sum; } }
Storage is expensive because it writes data permanently on the blockchain.
Memory is cheaper and temporary, good for calculations inside functions.
Calldata is the cheapest for input data but is read-only.
Storage keeps data permanently on the blockchain.
Memory is temporary and used inside functions.
Calldata is read-only input data, cheaper than memory.
Practice
storage in Solidity?Solution
Step 1: Understand storage role
storagevariables are saved permanently on the blockchain and persist between function calls.Step 2: Compare with memory and calldata
memoryis temporary during function execution, andcalldatais read-only input data for external functions.Final Answer:
It stores data permanently on the blockchain. -> Option AQuick Check:
Storage = permanent data [OK]
- Confusing storage with memory as temporary
- Thinking calldata can be modified
- Assuming storage is read-only
Solution
Step 1: Identify parameter data location for external input
Function parameters from external callers that should not be modified usecalldata.Step 2: Recall other data locations
memoryis for temporary data inside functions,storageis persistent, andeventis unrelated to data location.Final Answer:
calldata -> Option AQuick Check:
External input = calldata [OK]
- Using memory for external parameters
- Confusing event with data location
- Using storage for function parameters
_data = [1, 2, 3]?
function process(uint[] calldata _data) external pure returns (uint) {
uint sum = 0;
for (uint i = 0; i < _data.length; i++) {
sum += _data[i];
}
return sum;
}Solution
Step 1: Understand calldata usage
The function receives an array_dataascalldata, which is read-only but accessible for reading elements.Step 2: Calculate sum of array elements
Sum of [1, 2, 3] is 1 + 2 + 3 = 6, which is returned.Final Answer:
6 -> Option BQuick Check:
Sum of input array = 6 [OK]
- Thinking calldata cannot be read
- Expecting compilation error due to calldata
- Confusing sum calculation logic
function updateData(uint[] calldata _input) external {
_input[0] = 10;
}Solution
Step 1: Check mutability of calldata
calldatais read-only, so assigning to_input[0]causes an error.Step 2: Validate other options
The function can be non-pure, no return needed, and calldata arrays are allowed as parameters.Final Answer:
Cannot assign to calldata array elements. -> Option DQuick Check:
Calldata is read-only [OK]
- Trying to modify calldata elements
- Thinking pure is required here
- Assuming return is mandatory
Solution
Step 1: Choose parameter data location for large external input
calldatais efficient and read-only for large external arrays, avoiding unnecessary copying.Step 2: Choose storage variable for permanent data
To keep data permanently on blockchain, usestoragefor the variable that stores the processed result.Final Answer:
Parameter as calldata, storage variable as storage -> Option CQuick Check:
External input = calldata, permanent store = storage [OK]
- Using memory for large external input (inefficient)
- Trying to store permanent data in memory or calldata
- Confusing storage and memory roles
Ready to prove it?
Pass the test and this pattern moves unknown
