1

According to this answer, it says simpler variables like bool, uint, et cetera are not variable in length, and are therefore defaulted to memory, whereas this one mentions uint256 v; is default to storage. So I'm kinda confused whether the primitive types(int, uint, bool) used in the function is set to memory or storage as its default type. I know array is default to storage, ex: uint256[] x.

Anyone can clarify please?

0

According to the development Solidity docs, the location of local variables within a function's scope appears to indeed be storage.

However, this would seem contradictory to the usage of the pure function modifier as the following function:

function check() public pure returns (uint256) {
    uint256 a = 2;
    return a;
}

Contains completely valid syntax. The pure keyword is valid for functions that do not modify the contract's state or access it, meaning that the uint256 a is impossible to refer to a storage variable.

In order to validate the above assumption, I conducted a test with the following code snippets:

Storage Variable Assignment:

pragma solidity ^0.4.23;

contract StorageVariableTest {
    uint256 k;

    function assignValue() public {
        k = 2;
    }
}

Local Function Variable Assignment:

pragma solidity ^0.4.23;

contract LocalVariableTest {
    function assignValue() public {
        uint256 k = 2;
    }
}

Testing those functions on my Ganache blockchain showcased a lower cost for the second contract snippet, which indicates that the uint256 k = 2 declaration is never stored within the contract's storage/state.

As such, the correct explanation is that only variables declared explicitly as storage or that exist outside a function's scope are in fact storage variables.

Screenshot of Gas Costs:

Ganache Screenshot

1

It is confusing indeed. The docs state that by default:

Parameters (also return) of functions: memory.
All other local variables: storage

But variables defined in the scope of a function are not persistent (they are not accessible after the function is executed).

In terms of gas (using the code below) executing f will have a transaction cost of about 42K gas with execution cost of 20500, which is expected as we are storing permanently a value in storage.

pragma solidity ^0.4.23;

contract Mycontract{
    uint256 public x;

    function f(uint256 value) public returns(uint256){
        x = value;
        return x;
    }

}

But the following code:

contract Mycontract{

    function f(uint256 value) public pure returns (uint256){
        uint x; 
        x = value*2;
        return x;
    }

}

will cost have a transaction cos of about 21K gas with transaction cos of about 300 gas, which indicates we are not writing on the constract's storage. And definitely x is not accessible outside the function.

0

Perhaps we are confusing storage variables with state variables and using them interchangeably, which is not the case. Not all variables stored in storage will become state variables and ultimately be written in the blockchain. the local storage variable (or the local array pointer referencing a state variable) is not stored in state. Thus once the function is done executing, they will simply not be written in blockchain and hence has a lower gas consumption.

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.