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
| Location | Lifetime | Cost | Notes |
|---|---|---|---|
| storage | Permanent (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 |
| memory | During the function call | Cheap, grows quadratically | Temporary arrays/structs, function params of public functions |
| calldata | Read-only input of external calls | Cheapest for inputs | Use for external function array/string params |
| stack | Local value-type variables | Cheapest | 1024 slots max depth; "stack too deep" with too many locals |
| transient (EIP-1153, Solidity 0.8.28+) | Until end of the transaction | Cheap | Reentrancy 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];
}