solidity – What Are the First 128 Bytes in Solidity Memory Reserved For

opcodesolidity

In most solidity contracts, the first few opcodes generate the free memory pointer

PUSH1 0x80 
PUSH1 0x40 
MSTORE

This places the 0x80 value (128) at location 0x40 in memory. So, why don't we start the free memory pointer at 0x00? Why do we place it 0x40 down.

And additionally, why do we say that the free memory starts at 0x80 when the pointer is only 1 byte?

Best Answer

Per the solidity documentation

Solidity reserves four 32-byte slots, with specific byte ranges (inclusive of endpoints) being used as follows:

  • 0x00 - 0x3f (64 bytes): scratch space for hashing methods
  • 0x40 - 0x5f (32 bytes): currently allocated memory size (aka. free memory pointer)
  • 0x60 - 0x7f (32 bytes): zero slot