0
0
Blockchain / Solidityprogramming~10 mins

Storage vs memory vs calldata in Blockchain / Solidity - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Storage vs memory vs calldata
Function Call
Parameters Passed
Calldata (Read-only, cheap)
Memory (Temporary, modifiable)
Storage (Persistent, expensive)
Function Execution
End of Execution
When a function is called, inputs come as calldata (read-only). Data can be copied to memory (temporary, modifiable). Storage holds persistent data on blockchain, which is expensive to read/write.
Execution Sample
Blockchain / Solidity
contract Example {
  uint storedData;
  function setData(uint x) public {
    uint temp = x; // memory
    storedData = temp; // storage
  }
}
This contract sets a stored value by copying input from calldata to memory, then to storage.
Execution Table
StepVariableLocationValueAction
1xcalldata5Function called with input 5
2tempmemory5Copy x from calldata to memory
3storedDatastorage5Write temp value to storage
4---Function execution ends
💡 Function ends; calldata is discarded, memory cleared, storage keeps value
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
x (calldata)-555-
temp (memory)--55-
storedData (storage)00055
Key Moments - 3 Insights
Why can't we modify calldata variables directly?
Calldata is read-only as shown in step 1 of execution_table; to modify data, we copy it to memory (step 2).
Why is storage more expensive than memory?
Storage persists on blockchain and costs gas to read/write (step 3), while memory is temporary and cheaper.
What happens to memory and calldata after function ends?
Both calldata and memory are cleared after execution (exit_note), only storage keeps data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'temp' after step 2?
A-
B0
C5
DUndefined
💡 Hint
Check the 'Value' column for 'temp' at step 2 in execution_table
At which step is the value written to storage?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when 'storedData' changes
If we tried to modify 'x' directly, what would happen?
AIt would cause an error because calldata is read-only
BIt would modify calldata successfully
CIt would modify storage instead
DIt would modify memory automatically
💡 Hint
Refer to key_moments about why calldata is read-only and step 1 in execution_table
Concept Snapshot
Storage vs Memory vs Calldata in blockchain:
- Calldata: Read-only input, cheapest, temporary
- Memory: Temporary, modifiable during function
- Storage: Persistent, expensive, saved on blockchain
Copy calldata to memory to modify; write to storage to save permanently.
Full Transcript
When a blockchain function is called, inputs come as calldata, which is read-only and cheap. To change data, we copy it to memory, which is temporary and modifiable during the function. Storage holds data permanently on the blockchain and is expensive to read or write. In the example, input 'x' is received in calldata, copied to memory as 'temp', then saved to storage as 'storedData'. After the function ends, calldata and memory are cleared, but storage keeps the data. This flow helps manage cost and data persistence efficiently.