29

I am learning the inline assembly in solidity, I have found the function mload(0x40) I am confused with what this function does. Did it reserve 4 Slot/word in memory or did it load the data stored in the address 0X40 or what?

3 Answers 3

25

mload(0xAB) Loads the word (32byte) located at the memory address 0xAB. e.g mload(0x60) loads the word located at 0x60 memory address.

let's code to understand more :

  function f ()  
     {
       
         assembly {
             let freemem_pointer := mload(0x40)
             mstore(add(freemem_pointer,0x00),"36e5236fcd4c61044949678014f0d085")
             mstore(add(freemem_pointer,0x20),"36e5236fcd4c61044949678014f0d086")
             let arr1:= mload(freemem_pointer) //read first string 
             mstore(add(freemem_pointer,0x40),arr1)
    
            
         }
     }

this results in the flowing memory state :

enter image description here

This previous code stores two strings (two memory words 32bytes each) in the free memory space. The destinations memory addresses are obtained by adding an offset of 0bytes for the first and of 0x20 for the second to the free memory pointer address(located in the memory address 0x40).
in the EVM The 6 first words in the memory are reserved and the 0x40-0x50 memory words are allocated to the free memory pointer.

Details : mload and mstore are defined in details in the yellow paper :

enter image description here

[a...b) signifies the bytes of memory starting at position a up to (excluding) position b. however b] means b included

8
  • 3
    You really shouldn't just write to memory without checking the 0x40 pointer first. Oct 26, 2016 at 11:34
  • What are the first 4 words used for, if the last 2 are for the free memory pointer and the length of the dynamic array? Sep 27, 2018 at 18:23
  • 1
    @PaulRazvanBerg the first 4 are reserved for hash operations. Feb 1, 2020 at 2:34
  • 1
    Thanks @AntonCheng. For posterity: go read the Layout in Memory docs. They helped me understand this. Aug 30, 2021 at 16:22
  • What tool are you using to display the memory pane? Looking for some visual tools to help me grasp the concepts of evm. Nov 27, 2021 at 12:50
31

As others have stated, the x m:=mload(0x40) instruction reads the 32 bytes of memory starting at position 0x40.

In solidity, the 0x40 slot in memory is special: it contains the "free memory pointer" which points to the end of the currently allocated memory.

When you use inline assembly, you should load the data stored at 0x40 and then only write to addresses after the result. When you're done, if you want to keep that memory allocated, you should overwrite 0x40 with the new value of the free memory pointer.

2
  • 4
    Thanks for that very insightful remark. I have always been wondering why almost any contract starts with PUSH1 60 PUSH1 40 MSTORE, but now it makes sense, 60 is the free memory pointer.
    – SCBuergel
    May 4, 2017 at 10:54
  • 1
    @SCBuergel why 60, not 40?
    – DiveInto
    Feb 23, 2022 at 7:11
0

Those who might be confused from the picture posted by @Badr, although I am not sure why, the memory doesn't look like it currently (version ^0.8.0). As others pointed out, the mload(0x40) returns where the pointer addresses that you can start using it. (free-pointer)

The first four 32 bytes (128) are always reserved when your smart contract is deployed. This is the reason your mload(0x40) returns 80. 80 is represented in hex format. It is 128 in decimal, which is where you can start writing in memory! This is how the memory looks like currently.

enter image description here

2

Your Answer

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

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