Storage and memory are two ways to keep data in blockchain programs. Storage keeps data safe for a long time, while memory is used for quick, temporary data during a task.
Storage vs memory usage in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Blockchain / Solidity
// Solidity example
contract Example {
uint[] storageArray; // stored permanently
function useMemory() public {
uint[] memory tempArray = new uint[](5); // temporary memory
}
}Storage means data saved on the blockchain permanently.
Memory means temporary data used during function execution only.
Examples
Blockchain / Solidity
// Storage example
uint[] storageArray;
storageArray.push(10);Blockchain / Solidity
// Memory example
function tempData() public {
uint[] memory tempArray = new uint[](3);
tempArray[0] = 1;
}Sample Program
This contract shows how to add data permanently to storage and how to use temporary memory to calculate a sum.
Blockchain / Solidity
pragma solidity ^0.8.0; contract StorageVsMemory { uint[] public storedData; function addToStorage(uint value) public { storedData.push(value); // saved permanently } function processMemory() public pure returns (uint) { uint[] memory temp = new uint[](3); temp[0] = 5; temp[1] = 10; temp[2] = 15; uint sum = 0; for (uint i = 0; i < temp.length; i++) { sum += temp[i]; } return sum; // returns 30 } }
Important Notes
Using storage costs more gas because it saves data permanently on the blockchain.
Memory is cheaper but data is lost after the function finishes.
Choose storage for data you want to keep, memory for temporary calculations.
Summary
Storage keeps data permanently on the blockchain.
Memory holds temporary data during function execution.
Use storage for saving important data, memory for quick, temporary use.
Practice
1. Which of the following best describes
storage in blockchain smart contracts?easy
Solution
Step 1: Understand storage purpose
Storage holds data permanently on the blockchain, so it remains after function execution.Step 2: Compare with memory
Memory is temporary and only lasts during function execution, unlike storage.Final Answer:
Permanent data saved on the blockchain that persists between function calls. -> Option AQuick Check:
Storage = permanent data [OK]
Hint: Storage keeps data after functions finish, memory does not [OK]
Common Mistakes:
- Confusing memory with storage permanence
- Thinking storage is temporary
- Assuming storage is off-chain
- Believing storage is encrypted by default
2. Which of the following is the correct way to declare a variable in Solidity that uses
memory?easy
Solution
Step 1: Recall Solidity syntax for memory arrays
In Solidity, the correct syntax istype[] memory variableName;for memory arrays.Step 2: Check each option
uint[] memory numbers; matches the correct syntax:uint[] memory numbers;. Others have wrong order or keywords.Final Answer:
uint[] memory numbers; -> Option DQuick Check:
Memory arrays use 'type[] memory name' syntax [OK]
Hint: Memory keyword comes after type and brackets in Solidity [OK]
Common Mistakes:
- Placing 'memory' before type
- Using 'storage' instead of 'memory'
- Incorrect array syntax
- Missing brackets for array type
3. What will be the output of the following Solidity function?
pragma solidity ^0.8.0;
contract Test {
uint[] storageArray;
function addAndReturn() public returns (uint) {
uint[] memory tempArray = new uint[](1);
tempArray[0] = 5;
storageArray.push(10);
return tempArray[0];
}
}medium
Solution
Step 1: Analyze tempArray usage
The function creates a memory array with one element set to 5, then returns that element.Step 2: Understand storageArray effect
storageArray.push(10) adds 10 to storage but does not affect the returned value.Final Answer:
5 -> Option CQuick Check:
Return value from memory array = 5 [OK]
Hint: Return value from memory array, storage change doesn't affect output [OK]
Common Mistakes:
- Confusing storage push with return value
- Expecting storageArray value as output
- Thinking memory array persists after function
- Assuming compilation error due to storage usage
4. Identify the error in this Solidity function that tries to copy a storage array to memory:
pragma solidity ^0.8.0;
contract Example {
uint[] storageArray;
function copyArray() public view returns (uint[] memory) {
uint[] memory tempArray = storageArray;
return tempArray;
}
}medium
Solution
Step 1: Understand storage to memory assignment
In Solidity, you cannot directly assign a storage array to a memory array variable.Step 2: Identify correct copying method
You must copy elements manually or use a loop to transfer data from storage to memory.Final Answer:
Cannot assign storage array directly to memory array. -> Option AQuick Check:
Direct storage to memory assignment is invalid [OK]
Hint: Storage arrays need manual copying to memory arrays [OK]
Common Mistakes:
- Trying direct assignment from storage to memory
- Ignoring need for loops to copy arrays
- Assuming view functions can modify storage
- Believing memory arrays can't be returned
5. You want to write a Solidity function that temporarily modifies an array for calculations without changing the stored data. Which is the best approach?
hard
Solution
Step 1: Understand temporary modification needs
To avoid changing stored data, use a memory copy for temporary changes.Step 2: Evaluate options
Copy the storage array to a memory array, modify the memory array, then discard it. copies storage to memory, modifies memory, and discards changes, preserving storage.Step 3: Reject incorrect options
A declares memory in state (invalid syntax); B risks permanent changes if revert fails; C modifies storage directly.Final Answer:
Copy the storage array to a memory array, modify the memory array, then discard it. -> Option BQuick Check:
Use memory copy for temporary changes [OK]
Hint: Use memory copy to avoid changing stored data permanently [OK]
Common Mistakes:
- Modifying storage directly for temporary needs
- Trying to declare storage variables as memory in contract
- Assuming revert undoes all changes safely
- Using storage pointers for temporary changes
