3

The following opcodes 0x40 is "offset in the memory in bytes"

My question is why evm uses 0x80 as a "32-byte value to write in the memory."

PUSH1 0x80

PUSH1 0x40

MSTORE

Sincerely

3 Answers 3

3

The free memory pointer (stored at 0x40) starts at 0x80 simply because there are 4 32 byte slots at the start of memory that are reserved. From the Solidity docs on the memory layout:

  • 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

If the free memory pointer started any earlier than 0x80, it would interfere with these reserved slots (including the free memory pointer itself)

2

First of all what exactly is a free memory pointer ?

Free memory pointer points to a place where we can store whatever we want in memory right now meaning; we can store something right there in that location without worrying if that place is used by something else.

Now where is the place in memory reserved to the store this free location where anything can be stored ? aka free memory pointer

From the docs:

  • 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

0x7f is 127

0x80 is 128

  • As we can see that till 0x7f it's reserved for certain stuffs but from 0x80 we can store anything; So this is our free memory location where we can store anything! Now where to store this free memory location?

From the solidity docs: 0x40 - 0x5f (32 bytes): currently allocated memory size (aka. free memory pointer)

So as we can see that 0x40 is the place to store our free memory location which is 0x80. And 0x40 becomes our free memory pointer.

Again, remember that 0x40 is not the free memory location where we can store anything we want, it points to a place where we can store anything we want. Aka free memory pointer.

Here's a more in-depth explanation of this: https://pvnotpv.github.io/posts/runtimeinitcode/

0

Free memory start location is 0x40,

0x80 value indicates this location.

0x80=128 decimals is equal to 64 bytes

64 bytes is equal to 0x40

0x80 is decimal value but it indicates 0x40 location

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.