According to the Solidity Documentation, for the following code,

". . . the compiler interprets x as a storage pointer and will make it point to the storage slot 0 by default. This has the effect that someVariable (which resides at storage slot 0) is modified by x.push(2)".

/// THIS CONTRACT CONTAINS AN ERROR
pragma solidity ^0.4.0;

contract C {
    uint someVariable;
    uint[] data;

    function f() public {
        uint[] x;
        x.push(2);
        data = x;
    }
}

In another post, user smarx pointed out that:

"When you declare a storage variable, it's essentially a reference to some location in storage. Until you assign it to something, it points to location 0, which also happens to be the location of the first declared state variable (in this case a). You're basically using an uninitialized pointer".

What confuses me, however, is that if "uint [] x" points to 0, where "someVariable" resides, wouldn't "uint[] data" do the same? Aren't both of these arrays uninitialized? If "data" occupies slot 1, wouldn't "x" just occupy slot 2?

My guess is that when it is within a function, it starts from 0 again, assuming that everything will be saved to memory, but then I would expect an array declared within a function to reference memory by default.

up vote 1 down vote accepted

What confuses me, however, is that if uint [] x points to 0, where someVariable resides, wouldn't uint[] data do the same?

No, data would points to the location 1.

Aren't both of these arrays uninitialized?

yes, but the second one is pointing to an uninitialized storage location which is 0 by default (since storage is not dynamically allocated). As per doc:

The type of the local variable x is uint[] storage, but since storage is not dynamically allocated, it has to be assigned from a state variable before it can be used. So no space in storage will be allocated for x, but instead it functions only as an alias for a pre-existing variable in storage.

.

If data occupies slot 1, wouldn't x just occupy slot 3?

No, since is not allocated dynamically. But if would be allocated dynamically its reference will be stored in the slot 2 not 3.

  • I know data points to location 1, I'm just not sure why it doesn't point to 0. And I did mean to say "2", not "3". Anyways, the Solidity documentation is what confused me a bit. What is meant by "dynamically allocated" in this context? I thought it meant one thing, but it seems to mean something different. This response suggests that "data" and "someVariable" are dynamically allocated, but how can that be if they are in storage and "storage is not dynamically allocated"? Also, what is meant by "uninitialized" - I don't understand how "data" is initialized but "x" is not. – CreatedAMadman Jun 18 at 5:26
  • 1
    data has a initialized storage pointer (since is a state variable) while x no (so is pointing at 0). Only state variables are allowed to be stored inside the storage, local variable can point to storage locations (in case of arrays or structs) but can't be defined as new storage pointer inside the function because dynamical allocation is not allowed – mirg Jun 18 at 6:15

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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