5

I am reading the solidity docs here and I seem to be struggling to understand the following.

Data locations are important because they change how assignments behave: assignments between storage and memory and also to a state variable (even from other state variables) always create an independent copy. Assignments to local storage variables only assign a reference though, and this reference always points to the state variable even if the latter is changed in the meantime. On the other hand, assignments from a memory stored reference type to another memory-stored reference type do not create a copy

1)Isn't a state variable always stored in the storage? So what does the above mean by assignments between storage and memory and also to a state variable?

2) What exactly is a local storage variable?

pragma solidity ^0.4.0;

contract C {

    uint state_variable; // same as global storage variable

    function test() returns uint{
        uint local_variable = 10; //same as local storage variable
        return local_variable * state_variable;
    }
}

Is my understanding above correct? Is there a way to see if these variables are in memory or storage , I am looking at the disassembled op codes in remix IDE but unable to make it out.

Thanks!

0

2 Answers 2

2

Value assignment to a variable can be done by either giving a reference or creating an independent copy. If it's a reference when the original one which is assigned to the variable get changed value of the variable also gets changed. Consider the following,

a = 10 // value of a is 10
b = a // variable b is assigned a value
a = 20

if the assignment above assigned a reference now the value of b will also be 20. If the assignment was done by creating an independent copy value of b will be still 10.

In the solidity doc what it says is if the assignment is between the storage and memory,or the assignment is to a state variable it will always create an independent copy. If it's a local storage variable it will only make a reference.

Referring to the example provided in the solidity docs, local storage variable is a variable defined inside a function. (The scope of the variable is limited to the function)

1

Apart from the accepted answer, I think it's needed to identify what data locations are as well.

Documentation provides a good description on available data locations.

Data location

Every reference type has an additional annotation, the “data location”, about where it is stored. There are three data locations: memory, storage and calldata. Calldata is a non-modifiable, non-persistent area where function arguments are stored, and behaves mostly like memory. It is required for parameters of external functions but can also be used for other variables.

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.