In this track: Storage, memory and layout
advanced · 4 MIN READ

Struct storage layout

A struct starts a fresh slot and packs its own members.

LET’S PICTURE IT

A labeled divider starts a new drawer section, even if the earlier section has spare space.

SEE IT FOR YOURSELF
Pack small values into Solidity storage slotsStep 1 / 4
Pack small values into Solidity storage slotsFirstuint128 A16 bytesSeconduint128 B16 bytesThirduint256 C32 bytesSimple elementary types only; not a complete compiler layoutSlot 0 · 32 bytesUnused · 16 BA · 16 BHigh-order bytes ← → low-order bytesSlot 0 · fullB · 16 BA · 16 BDeclaration order and sizes determine this packingSlot 0BASlot 1C · 32 bytesTry different sequences in the storage-layout tool

In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

Start the struct on a boundary

A struct begins a new slot in ordinary Solidity layout. Its first member does not simply fill an unused fragment after an earlier standalone variable.

Pack compatible members inside

Two uint128 members can share the struct’s first slot. A following uint256 member occupies another slot because it needs the entire word.

Respect the boundary afterward

A variable following a struct begins a new slot. It does not reuse a leftover fragment in the struct’s final word merely because the type would fit.

Verify nested layouts

A struct containing arrays, mappings or other structs introduces further layout rules. Use compiler storage output for the exact declaration and inheritance context.

Inspect the layout

struct Pair { uint128 a; uint128 b; }
uint8 beforePair; // slot 0
Pair pair;       // slot 1
uint8 afterPair; // slot 2

A declaration sketch with no inherited state or custom base.

TURN UNDERSTANDING INTO PRACTICEOpen the Storage layout →
QUICK CHECK · NO PRESSURE

Can a following standalone field reuse a struct’s leftover tail bytes?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.