4

enter image description here

Let's say I have three state variables in my contract

contract A {
    uint private x = 255;
    uint[] y; 
    uint z;
}

As I understand it, x will have index 0, y index 1 and z index 2? Is it that simple?

How many indexes there are?

Are there special indexes?

Does index point to a single storage cell holding one variable(irrespective of its size) or does it point to a cell with a limit(like 32 bytes)?

I'm just trying to access private state variables at contract addresses and have no idea how. I believe it's done through this app method. Thank you!

1 Answer 1

6

An index will only retrieve a single storage slot which is 32 bytes.

y is a dynamically sized array, so it will not be at index 1.

The information you are looking for is:

https://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage

Statically-sized variables (everything except mapping and dynamically-sized array types) are laid out contiguously in storage starting from position 0.

...

Due to their unpredictable size, mapping and dynamically-sized array types use a sha3 computation to find the starting position of the value or the array data. These starting positions are always full stack slots.

(stack should be storage in the docs.)

Related: How do I get the storage indices/keys?

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.