Roadmap › Solidity & Smart Contract Development › Module: Language Basics
Lessons in this course (11)

3. Data locations: storage, memory, calldata, stack and transient

Basic★ Must knowLesson 3 of 11
LocationLifetimeCostNotes
storagePermanent (contract state)Most expensive (SSTORE ~20,000 gas for a new non-zero slot, 2,900–5,000 for updates; reads ~2,100 cold / 100 warm)State variables; storage pointers
memoryDuring the function callCheap, grows quadraticallyTemporary arrays/structs, function params of public functions
calldataRead-only input of external callsCheapest for inputsUse for external function array/string params
stackLocal value-type variablesCheapest1024 slots max depth; "stack too deep" with too many locals
transient (EIP-1153, Solidity 0.8.28+)Until end of the transactionCheapReentrancy locks, temporary approvals
solidity
struct Order { address buyer; uint96 amount; bool paid; }
Order[] public orders;

function markPaid(uint256 id) external {
    Order storage o = orders[id];     // reference to storage → modifies state
    o.paid = true;
}
function preview(uint256 id) external view returns (Order memory) {
    Order memory o = orders[id];      // copy into memory → changes would NOT persist
    return o;
}
function sum(uint256[] calldata xs) external pure returns (uint256 t) {
    for (uint256 i; i < xs.length; ++i) t += xs[i];
}