data locations
Picture three different desks where a contract can put data. Storage is the filing cabinet bolted to the floor: it survives between transactions and is by far the most expensive to use. Memory is a scratch pad wiped clean the instant the call ends. Calldata is the read-only envelope of arguments that arrived with the call, which you cannot alter at all. In Solidity, every reference-type variable (array, struct, string, bytes, mapping) must be tagged with one of these locations, because where the bytes live changes both the cost and the meaning of the code.
Value types (uint, bool, address) live on the EVM stack and need no annotation. Reference types must say where they are. Storage variables are the contract's persistent state and cost the most: a cold SLOAD is about 2,100 gas and writing a fresh storage slot is about 20,000 gas. Memory is byte-addressed temporary space whose cost grows quadratically as it expands. Calldata is the cheapest region: it is immutable and read straight from the transaction input, so passing big arrays as calldata avoids copying them into memory. Mappings can only ever live in storage.
The subtle part is assignment semantics. Copying a storage value into a memory variable (or vice versa) makes an independent copy. But a local variable declared as storage is a pointer (an alias) into actual state: writing through it writes to the chain. Beginners expect a copy and are surprised when their 'temporary' edit permanently changes the contract. Choosing calldata over memory for external function inputs, and avoiding needless storage-to-memory copies, is one of the first gas lessons every Solidity developer learns.
function f(uint[] calldata input) external {
uint[] memory temp = input; // calldata -> memory: a real copy
Item storage item = items[0]; // storage pointer (an alias!)
item.count++; // this writes to chain state
}Three locations, three behaviours, very different gas costs.
A local 'storage' variable is not a copy — it is an alias to live state. Writing to it mutates the chain; this trips up almost every beginner at least once.