1

The question in the title is a little vague, so I will try to explain here.

Suppose I have a contract with a storage array:

pragma solidity 0.4.24;

contract MyContract {
    uint256[10] public myArray;

    constructor() public {
        myArray[0] = ...;
        myArray[1] = ...;
        myArray[2] = ...;
        myArray[3] = ...;
        myArray[4] = ...;
        myArray[5] = ...;
        myArray[6] = ...;
        myArray[7] = ...;
        myArray[8] = ...;
        myArray[9] = ...;
    }
}

When I deploy this contract, can we say that myArray is allocated at a specific address, and that myArray occupies a consecutive memory block (in the example above, a block of 320 bytes) starting at that address?

If yes, is it guaranteed that no other state variable will reside at the same address range, and overlap that memory block (fully or partially)?

If the answer to all of the questions above is yes, how can I obtain the address-space (or at least the base-address) of myArray in Solidity?

The equivalence in native languages like C or C++, when applied in Solidity, would be something like address(myArray), I haven't tried it, but I'm pretty sure it won't work.

Thank you!

5

The answer to your title question (and subsequent assumptions) is no.

All variables from a contract are stored in the storage trie which lives under the contract's address. Storage is not given its own Ethereum address.

It seems like what you want to do is access specific parts of the contracts storage, which you can do with web3.eth.getStorageAt. Maybe somthing like this tutorial will help: How to read Ethereum contract storage

Essentially, each variable occupies 256 bytes, and are stored one after the other. So if you wanted the 4th item in your array, you would simply call:

web3.eth.getStorageAt(contractAddress, 3)

If you initialized other variables before the array, you would simply shift the storage index up by that many.

  • Thank you. You last assumption is wrong, because I am specifically interested in obtaining the address on-chain and not off-chain (i.e., in the contract, not in web3). But if I understand you correctly (and also from another answer that I found here), then the only "thing that has an address" is a contract. – goodvibration Dec 23 '18 at 12:25
  • @goodvibration I think the takeaway here is that the contract storage data itself isn't stored on-chain. Rather, hashes of that data are stored. In other words, nodes store this contract storage data, not the blockchain itself. – notlesh Dec 23 '18 at 16:45
  • Furthermore, the addresses that you are referring to are hashes of an account's public key -- you seem to be conflating this with memory address space in a traditional computing environment. – notlesh Dec 23 '18 at 16:48
  • @notlesh: I have definitely relied on traditional eco-systems in my analysis (I even mention that at some point in the question). – goodvibration Dec 23 '18 at 21:35

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.