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
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.
In this simple default-layout example, two uint128 values come before one uint256. Each slot holds thirty-two bytes.The first sixteen-byte value occupies the low-order half of slot zero. Low-order bytes are shown on the right.The second uint128 fits in the remaining sixteen bytes. Both values now share slot zero.C needs a complete thirty-two-byte slot, so it begins at slot one. Two packed small values plus C use two slots.
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.