Accessing farther memory can increase execution cost.
LET’S PICTURE IT
A worktable can be extended when a task reaches beyond its current usable area.
SEE IT FOR YOURSELF
Use temporary memory in one execution frameStep 1 / 4
This frame uses temporary byte-addressed memory. Solidity normally keeps a free memory pointer at address 0x40, initially pointing to 0x80.
This frame uses temporary byte-addressed memory. Solidity normally keeps a free memory pointer at address 0x40, initially pointing to 0x80.In this simplified example, MSTORE writes integer forty-two into thirty-two bytes beginning at memory byte 0x80. Accessing this range may expand active memory.MLOAD at byte 0x80 reads that complete thirty-two-byte word back as a stack value. It does not fetch persistent storage or network data.RETURN can expose a chosen memory range as output. Frame memory is temporary; returning data does not turn it into persistent contract state.
Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.
Separate memory from storage
EVM memory is temporary byte-addressed working space for an execution frame. It is not persistent contract storage and does not use Solidity’s storage packing layout.
Track the highest accessed range
An operation touching farther memory can expand the active memory region. Both the starting offset and the accessed size matter when deciding the new boundary.
Understand cumulative cost
Expansion cost depends on the new memory size and includes a quadratic component. Splitting expansion into several steps does not remove the cumulative expansion component for the same final size.
Avoid unbounded copies
A large user-influenced copy or allocation can consume substantial gas. Bound the work according to the application and test large inputs rather than assuming memory is free.
Inspect the layout
MSTORE at offset 0x80 writes bytes 0x80 through 0x9f.
Memory expansion depends on the accessed range, not merely the starting byte.
This memory example uses byte addresses, not storage slot indices.