freecoding.school100% FREE · NO SIGNUP
Ledger LandISSUE #31 of 90

solidity · storage vs memory vs calldata

BlockyVSDouble-Spend Dan
Blocky saysSolidity data lives in storage (persistent), memory (temporary), or calldata (read-only input).

Where a variable lives changes cost and behavior. storage persists on-chain (expensive, the contract's permanent state). memory is a temporary scratchpad cleared after the call (cheaper). calldata is the read-only, cheapest location for function arguments. Choosing the right location — e.g. calldata for external array params — is a key gas and correctness decision.

Power-ups you unlock

Double-Spend Dan attacks — common mistakes

Boss battleChoose the data location for an external function’s array parameter and explain why.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>function sum(uint[] calldata xs) external pure returns(uint){
  // calldata: cheapest, read-only input
}
storage = permanent · memory = scratch · calldata = input</pre></body></html>
▶ Open the interactive comic issue
‹ Solidity · Types · Functions · ModifiersVyper · The Python-Flavored Alternative ›