0

I do not understand the difference between mstore(0x40) and mstore(mload(ox40)). I know that mstore(mload(ox40), something) stores something in the first free 32bytes of the memory. What does mstore(0x40, something) do? Does it do the same thing without the boundry of 32bytes to store something ?

2
  • Check this out ethereum.stackexchange.com/questions/9603/… May 22, 2022 at 16:31
  • To be honest, that post made me ask this question :) I understand totally what is happening in that post. But as I said there is nothing about the distinction between mstore(0x40) and mstore(mload(ox40)) in that post.
    – ege126
    May 22, 2022 at 17:41

2 Answers 2

1

mstore(0x40, ...) stores at memPos 0x40

mstore(mload(0x40), ...) stores at the memPos stored at memPos 0x40. usually you store the free memory pointer at memPos 0x40. In a solidity contract the first value stored at 0x40 should be 0x80

1

Ok, so the opcode 'mstore' stores some value at a certain offset (memory position).

It takes 2 arguments mstore(offset, value) --> offset means at which memory position you want to store the value, and the value is just whatever value (uint256, bytes32, etc...)

For example, if you store the value 0x1234 at offset 0x80: mstore(0x80, 0x1234), you would have the value 0x1234 (zero-padded) starting at memory position 0x80 (taking 32 bytes). It is important to understand that you can store more than 32 bytes, it will just expand the memory further, what you cannot do is read more than 32 bytes.

Now, the opcode 'mload' takes 1 argument and that is the offset. In simpler words, it is a starting point in the memory array, and it will return the 32 bytes that follow. In our past example if you would want to read the value 0x1234 you would just need to call mload(0x80).

To answer precisely your question, the difference between 'mstore(0x40, something)', and 'mstore(mload(0x40), something)' is that the in the first one 'mstore(0x40, something)' you are storing the value 'something' at memory position 0x40.

In the second one 'mstore(mload(0x40), something)' you are storing something at position 'mload(0x40)', this simply means whatever value is at memory position 0x40 (0x40 is the free memory pointer).

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.