Skip to content
Back to Curriculum
FUNDAMENTALS

Types & Data Locations

Solidity is statically typed. Variables serve as containers for data. Crucially, reference types (arrays, structs) must specify a data location: `memory` (temporary), `storage` (persistent), or `calldata` (read-only input).

Syntax Structure

Example.sol
Loading...

Key Components

  • Boilerplate structure required by the Solc compiler.
  • Placement of state variables versus functions.

Practical Implementation

See the difference between modifying a Storage pointer (affects state) vs a Memory copy (discarded).

Practical.sol
Loading...

Core Concepts

Value Type

Stores data directly (e.g. uint, bool, address). Passed by value (copied).

Reference Type

Stores a pointer to data (e.g. array, struct, mapping). Can be passed by reference.

Data Location

The specific region of the EVM where data is stored. Affects persistence and gas cost.

Mental Model

The Warehouse vs. The Backpack

**Storage** is the Warehouse: modifying items here takes a forklift and updates the master inventory. **Memory** is your Backpack: you can take items out of the warehouse, put them in your bag, and modify them, but if you don't put them back, the changes vanish when you leave.

Under the Hood

`storage` variables are state pointers. `memory` is a linear byte array that expands. `calldata` is a non-modifiable, direct usage of the transaction input data area (cheapest). Copying from `calldata` to `memory` costs gas proportional to size (`identity` precompile or loop).

CALLDATALOADMLOADMSTORESLOADSSTORE

Gas Analysis

Wrong data location choices are the #1 source of gas rework.

  • • Use `calldata` for array parameters in external functions to avoid copying.
  • • Don't copy storage arrays to memory unless you need to loop over them many times.

Security Risks

Uninitialized storage pointers can point to slot 0 and overwrite critical variables.

  • ! Storage Pointer Bug: In older Solidity versions, uninitialized pointers could corrupt storage. (Fixed in 0.5.0, but concept remains important).

Common Use Cases

Optimizing gas usage by choosing the right location.
Preventing unintended storage modifications.
Handling large datasets efficiently.

Interactive Visualization

See exactly how the EVM handles this code in memory, storage, and the stack.

Interactive Simulation
0 / 0
Loading...
Type Solidity code (v0.8.26) and click Run. Compiler loads on first use (~2MB).

Click Run to execute the simulation

Then use the step controls to navigate