0

from solidity documentation

Assignments from storage to a local storage variable also only assign a reference.

In this contract, when I am intialising x in the function, it defaults to a data location in storage, so it must reference the data.

why deleting x is not deleting the data?

pragma solidity >=0.4.0 <0.7.0;

contract DeleteExample {
    uint data;
    uint[] dataArray;

    function f() public {
        uint x = data;
        delete x; // sets x to 0, does not affect data
        delete data; // sets data to 0, does not affect x
        uint[] storage y = dataArray;
        delete dataArray; // this sets dataArray.length to zero, but as uint[] is a complex object, also
        // y is affected which is an alias to the storage object
        // On the other hand: "delete y" is not valid, as assignments to local variables
        // referencing storage objects can only be made from existing storage objects.
        assert(y.length == 0);
    }
}

but when we initialized y with storage, it clearly referenced to the original dataarray. I was under the impression that local variables will always be stored on in-memory until storage is defined explicitly. am I wrong?

1 Answer 1

1

It's really not clear what you are asking.

when I am intialising x in the function, it defaults to a data location in storage

No. x is a memory variable. The scaler types default to memory, so x is not a storage variable. It is a bad idea to instantiate any storage variables (not pointers) inside functions even if the compiler seems to let you.

why deleting x is not deleting the data?

What data? I don't see x = in this function, so x refers to an unset uint, so 0.

I was under the impression that local variables will always be stored on in-memory until storage is defined explicitly. am I wrong?

Yes, for scaler types. For indexed types (structs, mappings, arrays) you must explicitly indicate memory or storage.

why deleting x is not deleting the data?

It doesn't seem like x or data was set to anything and it doesn't seem like you tested anything after delete x. It's unclear how you reached any conclusion or what you are attempting to prove.

This might possibly clear up some things.

pragma solidity >=0.4.0 <0.7.0;

contract DeleteExample {
    uint data = 10;
    uint[] array;

    function f() public {
        uint x = data;                              // to memory from storage
        delete data;                                // delete from storage
        require(x == 10, "it was affected.");
        x = data;                                   // update memory from storage
        require(x == 0, "it was not deleted.");
    }

    function g() public {
        uint[] storage mem = array;                 // storage pointer
        array.push(1);
        array.push(11);
        array.push(21);
        require(mem.length == 3, "not three elements.");
        require(mem[0]==1, "not one.");
        require(mem[1]==11, "not eleven.");
        require(mem[2]==21, "not twenty-one.");
        delete array[1];                            // delete from storage
        require(mem[1]==0, "element not gone.");    // storage pointer affected
    }
}

This blog might help: Storage Pointers in Solidity.

Hope it helps.

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.