Storage, memory and calldata are the three data locations in Solidity. Storage is the contract's permanent state on the blockchain, memory is temporary and writable data that exists only during an external call, and calldata is temporary read-only data holding the arguments of an external call. The solidity storage memory calldata choice affects both correctness and gas cost, and the compiler forces you to state it for every reference-type variable.
This guide uses Solidity 0.8.x. Reference types are arrays, structs and mappings, plus string and bytes. Value types such as uint256, bool and address never need a location.
Every reference-type variable lives in exactly one data location, and the location decides its lifetime and cost.
| Location | Lifetime | Writable | Typical cost | Where it is used |
|---|---|---|---|---|
storage | Permanent, part of contract state | Yes | Highest | State variables, references to them |
memory | One external call or transaction step | Yes | Moderate | Local variables, copies, return values |
calldata | One external call | No | Lowest | Function arguments of external-facing functions |
Reading and writing storage uses SLOAD and SSTORE, which are among the most expensive opcodes. Memory is a byte array that grows during execution, and its cost grows with the amount you use. Calldata is the raw input of the call, so reading it costs very little.
State variables declared at contract level are always in storage. You cannot change that, so you never write storage on them.
Each push writes to storage and costs a lot of gas. Every storage slot holds 32 bytes, and the compiler packs smaller types such as uint128 and bool into one slot when they sit next to each other. Because storage is permanent, it is also the data that every node must keep, which is why it is expensive.
Inside a function you can declare a local variable with the storage keyword. It does not copy anything; it is a pointer to the original data.
If you had written Player memory p, the function would copy the struct, update the copy and then lose it, leaving the stored score unchanged. This is the most common beginner bug with data locations.
Memory holds local variables and values you build during a call. It is erased when the external call ends. You create new arrays in memory with new, and their length is fixed at creation.
Memory arrays cannot use push, because their size is fixed when you allocate them. If you need a growing list, build it in storage or compute the length first.
Calldata is where arguments of an external call arrive. It cannot be modified, and it avoids a copy when you only read the data.
With calldata, the function reads values straight from the transaction input. With memory, the compiler first copies them into memory, which costs extra gas on large arrays. Since Solidity 0.6.9 calldata is allowed for parameters of any function visibility, but external functions are where it is most natural.
Assignment between locations follows fixed rules, and mixing them up is a frequent source of confusion.
storage variable creates a reference, not a copy.calldata for read-only array, struct and string arguments of functions called from outside.memory when you need to modify an argument or build a new array or string.storage pointer when you want to update an existing stored struct or array element without copying it.Because the same type can live in different places with different cost and copy behaviour. Requiring the keyword forces you to decide, so a copy never happens by accident.
For read-only external arguments, yes, because no copy is made. If you must change the data, you need memory.
No. Types such as uint256, bool, address and bytes32 are always passed by value and need no keyword.
You most likely declared the local variable as memory, which copies the struct. Use storage to modify the original.
Storage is permanent and expensive, memory is temporary and writable, and calldata is temporary and read-only. Use calldata for external inputs you only read, memory for data you change or build, and storage pointers to edit stored data in place. Remember that storage to memory copies, while a local storage variable only points to the original.