I have this very simple contract:

    pragma solidity ^0.4.18;
    contract C{
    uint public a = 9;
    uint[] public data;

    function f(uint _a) public{
        a = _a;
        uint[] y;

I understand that local variables of type array (uint[] y) references storage, but I don't understand why y.push(2900) modifies storage variable named a, and no matter what value I push to y it always increments variable a with one unit. Thanks you

1 Answer 1


You are declaring y as a storage but you left unitialized so y points to the same slot than a.

The first 32 bytes of an array in storage is the array length, and after that follows the data.

So the first a = _a will set the array length. And then y.push(2900) will append a new value to the array incrementing its length.

  • Why does "so y points to the same slot as a" happens? Should not it be considered as a bug of solidity compiler? As per as I understand the usual convention in most programming language compiler design is that memory location of different variables should not overlap unless explicitly done so?
    – sourav
    Mar 11, 2018 at 3:46
  • 1
    @sourav Solidity is a ver simplistic language and allocation of storage variables is static, first variable gets slot 0, and so on. If you do not initialize a storage variable it will refer to slot 0. New versions of solc display a warning when used like that.
    – Ismael
    Mar 11, 2018 at 4:35
  • Thanks @Ismael. One question does in solidity all uninitialized variable refer to slot 0 or it refers to last assigned slot in the storage. For example, if I define 2 variables uint a = 1; uint b = 2; and then declare a variable like y in the above question. which slot y will refer to? 0 or one which is the last index allocated(1 in my example)
    – sourav
    Mar 11, 2018 at 7:25
  • @sourav Uninitialized storage variables will point to slot 0.
    – Ismael
    Mar 11, 2018 at 19:32

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.