Storage vs Memory
Data location.
Why Data Location Matters
In Solidity, reference types must specify a data location. The three locations are storage, memory, and calldata.
Choosing the right one affects persistence, mutability, and most importantly gas cost.
Storage
Storage is the contract's permanent on-chain state. State variables live here, and writing to storage is the most expensive operation.
Data in storage persists between transactions.
<code>contract Bank {
uint256 public total; // lives in storage forever
function add(uint256 v) public {
total += v; // writes to storage (expensive)
}
}</code>All lessons in this course
- Value Types
- Reference Types
- Storage vs Memory
- Constants and Immutables