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!
Best Answer
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,
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)