1

How to get length of foo[1] in example below in assembly?

uint[][] foo; // foo.length = 2, foo[1].length = 3

function getLength() returns (uint len) {
    assembly {
        len := sload(foo) // returns 2
        len := sload(........) // how to return foo[1] ?
    }
}
1

This guide explained it fairly well. The storage pointers for nested arrays are organized as nested hash functions. At level one, keccak256(foo_slot), are the lengths for all first order arrays, foo[0], foo[1], foo[2], etc. The length of each individual subarray is, correspondingly, keccak256(foo_slot), keccak256(foo_slot) + 1, keccak256(foo_slot) + 2, etc.

In the example asked about, the length of foo[1] is found with:

len := sload(add(keccak256(foo_slot, 32), 1)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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