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
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.
- 37,046
- 10
- 91
- 118
mload(0xAB) loads the word (32 bytes long) located at the memory address 0xAB. e.g mload(0x60) loads the word located at the 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)
}
}
After execution, f() generates the following memory state:

The code above stores two strings (two memory words of 32 bytes each) in the free memory space. The destination memory addresses are obtained by adding offsets of 0x00 and 0x20 (for the first and second strings respectively) to the free memory pointer address (located at the memory address 0x40 and pointing to the 0x60 address).
In the EVM, the first 4 words in the memory are reserved and the 0x40-0x50 memory words are allocated to the free memory pointer.
MLOAD and MSTORE are defined in detail in the Ethereum Yellow Paper:
[a...b) refers to the bytes of memory starting at position a up to (but excluding) position b. However, b] means "b included".
- 301
- 1
- 2
- 19
- 18,780
- 4
- 58
- 75
-
4You really shouldn't just write to memory without checking the
0x40pointer first. – Tjaden Hess Oct 26 '16 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? – Paul Razvan Berg Sep 27 '18 at 18:23
-
1
-
2Thanks @AntonCheng. For posterity: go read the Layout in Memory docs. They helped me understand this. – Paul Razvan Berg Aug 30 '21 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. – Alex Lacayo Nov 27 '21 at 12:50
-
What are the numbers between the memory address and the string values? the 333665... etc. What do these values represent? – user1099123 Dec 30 '21 at 14:59
-
-
@BadrBellaj Can you expand a little more on what those string values represent ? – Rodrigo Herrera Itie Sep 08 '22 at 21:08
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.
- 1,654
- 2
- 9
- 24
-
Is that picture from remix? If so, how did you generate it? Is that some sort of plugin? Thanks a lot! – Kuly14 Jul 18 '22 at 13:27
-
Hi, no plugin. When the transaction is created, use debug option and it will show you. https://remix-ide.readthedocs.io/en/latest/tutorial_debug.html – Emrah Jul 18 '22 at 13:32


PUSH1 60 PUSH1 40 MSTORE, but now it makes sense,60is the free memory pointer. – SCBuergel May 04 '17 at 10:5460, not40? – DiveInto Feb 23 '22 at 07:11