Data Locations
Value types like uint256 are simply copied around. Reference types (arrays, string, bytes, structs) can be large, so Solidity makes you say where each one lives. There are three data locations, and choosing the wrong one is a common source of both bugs and wasted gas.
| Location | Where | Lifetime | Mutable | Cost |
|---|---|---|---|---|
storage |
the contract's permanent key-value store | forever | yes | very expensive to write |
memory |
a temporary byte array for the current call | until the call ends | yes | cheap |
calldata |
the read-only input data of an external call | until the call ends | no | cheapest |
State variables are always in storage. For function parameters, return values and local variables of a reference type you must write memory, calldata or storage explicitly.
memory vs storage: copy or reference?
The key rule: assigning between storage and memory makes a copy, while a local storage variable is a reference (a pointer) to the state.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
uint256[] public scores = [10, 20, 30];
function main() external {
uint256[] memory copy = scores; // copy storage -> memory
copy[0] = 999; // changes only the copy
console.log("after editing the memory copy:", scores[0]);
uint256[] storage ref = scores; // reference to the state variable
ref[0] = 999; // changes the state
console.log("after editing the storage ref:", scores[0]);
}
}after editing the memory copy: 10
after editing the storage ref: 999
The same applies to structs. Forgetting storage and editing a memory copy is a classic bug: the function runs fine, but nothing is saved.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Main {
struct Player { string name; uint256 level; }
Player[] public players;
function levelUpBuggy(uint256 i) internal view {
Player memory p = players[i]; // a copy!
p.level += 1; // lost when the function returns
}
function levelUp(uint256 i) internal {
Player storage p = players[i]; // points at the stored struct
p.level += 1;
}
function main() external {
players.push(Player("Ada", 1));
levelUpBuggy(0);
console.log("after buggy version:", players[0].level);
levelUp(0);
console.log("after fixed version:", players[0].level);
}
}after buggy version: 1
after fixed version: 2
Notice that the compiler even allows levelUpBuggy to be view: it never touches storage, which is a strong hint something is wrong.
calldata for inputs
For external functions, array and string parameters can stay in calldata. They are read directly from the transaction input, without copying into memory, which saves gas. The price: they are read-only.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "hardhat/console.sol";
contract Stats {
function sum(uint256[] calldata values) external pure returns (uint256 total) {
for (uint256 i = 0; i < values.length; i++) {
total += values[i];
}
// values[0] = 1; // compile error: calldata is read-only
}
function doubled(uint256[] memory values) public pure returns (uint256[] memory) {
for (uint256 i = 0; i < values.length; i++) {
values[i] *= 2; // memory copies may be modified
}
return values;
}
}
contract Main {
function main() external {
Stats stats = new Stats();
uint256[] memory data = new uint256[](4);
data[0] = 3; data[1] = 1; data[2] = 4; data[3] = 1;
console.log("sum :", stats.sum(data));
uint256[] memory twice = stats.doubled(data);
console.log("doubled:", twice[0], twice[1], twice[2]);
console.log("original unchanged:", data[0]);
}
}sum : 9
doubled: 6 2 8
original unchanged: 3
When Main calls stats.doubled(data), the array is ABI-encoded into a message and decoded into Stats' own memory, so Stats modifies its own copy and Main's data is untouched. External calls never share memory.
Quick rules
| Situation | Use |
|---|---|
| External function parameter you only read | calldata |
Parameter you need to modify, or a public/internal function parameter |
memory |
| Temporary array/struct built in a function | memory |
| Edit an element of a state array or mapping in place | local storage reference |
| Return a reference type | memory |
Memory arrays have a fixed size
Memory arrays are created with new T[](length) and cannot grow: push exists only for storage arrays. If you don't know the final size, count first and allocate once, or build the result in storage.
Test yourself
What does Player memory p = players[0]; p.level = 5; do to the stored player?
- Sets its level to 5
- Nothing;
pis a copy in memory - It does not compile
- It deletes the player
Which data location is read-only?
storagememorycalldata- All of them
Which location keeps data between transactions?
memorycalldatastorage- The stack