Understanding Reference Types Behavior in Blockchain Smart Contracts
📖 Scenario: Imagine you are building a simple blockchain smart contract to manage a list of users and their balances. You want to understand how reference types like arrays behave when you assign them to new variables or modify them inside functions.
🎯 Goal: You will create a smart contract that stores an array of balances. You will then create a function to demonstrate how modifying a reference type affects the original data. This will help you understand reference types behavior in Solidity.
📋 What You'll Learn
Create a balances array with initial values
Create a tempBalances variable to hold a reference to balances
Write a function to modify tempBalances and observe changes in balances
Print the balances before and after modification
💡 Why This Matters
🌍 Real World
Understanding reference types is important when writing smart contracts that manage data efficiently and correctly on the blockchain.
💼 Career
Blockchain developers must know how data is stored and modified in smart contracts to avoid bugs and security issues.
Progress0 / 4 steps
1
Create the initial balances array
Create a public uint[] array called balances with these exact values: 100, 200, and 300.
Blockchain / Solidity
Hint
Use uint[] public balances = [100, 200, 300]; inside the contract.
2
Create a reference variable to balances
Inside the contract, create a public function called setTempBalances that declares a local uint[] storage variable named tempBalances and assign it to balances.
Blockchain / Solidity
Hint
Use uint[] storage tempBalances = balances; inside the function.
3
Modify the reference variable
Inside the setTempBalances function, add code to change the first element of tempBalances to 999.
Blockchain / Solidity
Hint
Use tempBalances[0] = 999; to modify the first element.
4
Check the updated balances
Add a public view function called getBalances that returns the balances array. This will show the updated balances after calling setTempBalances.
Blockchain / Solidity
Hint
Use function getBalances() public view returns (uint[] memory) { return balances; }.
Practice
(1/5)
1. In blockchain programming, what happens when you assign one reference type variable to another?
easy
A. The variables become independent and unrelated.
B. A new copy of the data is created.
C. The original data is deleted.
D. Both variables point to the same data location.
Solution
Step 1: Understand reference assignment
Assigning one reference type variable to another makes both point to the same data location in memory.
Step 2: Effect of changes through references
Changes made through either variable affect the same original data because they share the reference.
Final Answer:
Both variables point to the same data location. -> Option D
Quick Check:
Reference assignment shares data location = A [OK]
Hint: Reference types share data location, not copy data [OK]
Common Mistakes:
Thinking assignment copies data
Assuming variables become independent
Believing original data is deleted
2. Which of the following is the correct syntax to copy a reference type variable in Solidity (blockchain language)?
easy
A. uint[] memory newArray = oldArray;
B. uint[] newArray = oldArray.copy();
C. uint[] storage newArray = oldArray;
D. uint[] newArray = copy(oldArray);
Solution
Step 1: Understand storage vs memory in Solidity
Storage variables hold data permanently on blockchain, memory variables are temporary copies.
Step 2: Correct way to copy reference
Assigning a storage reference variable to another storage variable copies the reference, not the data.
Final Answer:
uint[] storage newArray = oldArray; -> Option C
Quick Check:
Storage assignment copies reference = B [OK]
Hint: Use 'storage' keyword to copy reference, not 'memory' [OK]
A. Length is unchanged from original someArray length.
B. Length increases by 1 due to push on arr2.
C. Length decreases by 1 due to push on arr2.
D. Code causes a compilation error.
Solution
Step 1: Understand reference assignment
arr2 is assigned as a storage reference to arr1, so both point to the same array.
Step 2: Effect of push on arr2
Pushing 10 to arr2 modifies the shared array, increasing its length by 1.
Final Answer:
Length increases by 1 due to push on arr2. -> Option B
Quick Check:
Shared reference means push affects both = C [OK]
Hint: Changes via one reference affect all references [OK]
Common Mistakes:
Thinking arr1 length stays same
Assuming arr2 is a copy
Ignoring reference sharing behavior
4. Identify the error in this Solidity code snippet:
function updateArray(uint[] memory arr) public { uint[] storage ref = arr; ref.push(5); }
medium
A. Cannot assign memory array to storage reference directly.
B. Cannot push to a storage array.
C. Function must be declared view.
D. No error; code runs fine.
Solution
Step 1: Check assignment between memory and storage
In Solidity, you cannot assign a memory array directly to a storage reference variable.
Step 2: Understand push operation
Push is allowed on storage arrays, but assignment error occurs before push.
Final Answer:
Cannot assign memory array to storage reference directly. -> Option A
Quick Check:
Memory to storage assignment invalid = A [OK]
Hint: Memory and storage types cannot be assigned directly [OK]
Common Mistakes:
Assuming memory can be assigned to storage
Thinking push is disallowed on storage
Ignoring data location mismatch
5. You want to create a function in Solidity that returns a modified copy of a storage array without changing the original. Which approach correctly uses reference types to achieve this?
hard
A. Create a new memory array, copy elements from storage, modify it, then return.
B. Assign storage array to a storage reference, modify it, then return.
C. Return the storage array directly after modifying it.
D. Use a global variable to store the modified array.
Solution
Step 1: Understand data locations for copying
To avoid changing original storage array, create a new memory array and copy elements.
Step 2: Modify and return the memory array
Modify the memory array safely and return it without affecting storage data.
Final Answer:
Create a new memory array, copy elements from storage, modify it, then return. -> Option A
Quick Check:
Use memory copy to avoid changing storage = D [OK]
Hint: Copy to memory, modify, then return to keep original safe [OK]
Common Mistakes:
Modifying storage reference directly
Returning storage array after modification
Using global variables unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.