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

Mapping to a struct

A mapping hash selects the struct’s base slot.

LET’S PICTURE IT

A directory lookup can locate a multi-drawer record rather than a single field.

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.

Derive the entry base

For a mapping value that is a struct, the key derivation still selects a base slot. It does not force every struct member into that one word.

Lay out members from that base

If the struct contains two uint128 fields then a uint256, the two small members share the base word and the full-width member uses the next slot.

Keep nested members recursive

An array or mapping inside that struct has its own declared relative position and hash-derived rules. Apply those rules after locating the containing struct.

Avoid a misleading single-slot label

A user record may span several slots even though one mapping key selects it. Label the base and each member offset separately in an inspection tool.

Inspect the layout

struct Record { uint128 a; uint128 b; uint256 c; }
base = hash(address key, mapping slot)
a and b: base
c: base + 1

The hash input uses the standard padded address-key layout described in the previous lesson.

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

Can one mapping value span multiple storage slots?

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.