1

I am trying to understand how https://solidity.readthedocs.io/en/v0.7.2/internals/layout_in_memory.html works.

function test(uint a) public {
   uint test1 = 20; 
   uint[1] testArr;
   testArr[0] = 20;
}

Question 1) where is test1 is stored ? I saw the article which states that all the local variables are stored in memory and not in stack. is this correct ?

Question 2) could you write assembly code with mload of how to get test1 and testArr[0] from memory since I don't understand how solidity explains memory layout. I'd really appreciate this.

1 Answer 1

1

The article is wrong. Value types like bool, uintXXX, bytesYY, etc. are stored in stack, for dynamic types you are forced to choose memory or storage.

To read the array data just mload the address. Fixed size data arrays only contains the data, they don't store the length at the beginning like dynamic arrays.

function test() public view returns (uint r) {
   uint[1] memory testArr;
   testArr[0] = 20;
   assembly {
       r := mload(testArr)
   }
}

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.