Assignment by Value vs Reference in Solidity

Assigning storage to memory is by value, i.e., value is copied over. Assigning memory to storage is also by value, i.e., value is copied over.

uint[] storage storageArray = [1, 2, 3];
uint[] memory memoryArray = storageArray; // value of storageArray is copied over to memoryArray
// modifying memoryArray will not change storageArray

Assigning memory to memory or storage to storage is by reference, i.e., the new variable simply points to the same data location in memory, no copy is created.

uint[] storage pointerArray = storageArray; // [1, 2,3]
pointerArray.push(9); // both point to same array of [1, 2, 3, 9]
Show Comments