Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Storage vs memory usage in Blockchain / Solidity - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Storage vs memory usage
Start Transaction
Check Data Type
Storage
Write to Disk
Cost Gas
End Transaction
This flow shows how blockchain handles data: deciding if data goes to storage (permanent, costly) or memory (temporary, cheaper).
Execution Sample
Blockchain / Solidity
contract Example {
  uint storedData;
  function set(uint x) public {
    storedData = x; // storage
    uint temp = x;  // memory
  }
}
This smart contract stores a number permanently and uses a temporary memory variable inside a function.
Execution Table
StepActionVariableLocationValueGas Cost Impact
1Function set called with x=5xInput5No
2Assign x to storedDatastoredDataStorage5Yes (high)
3Assign x to temptempMemory5No (low)
4Function ends, temp clearedtempMemoryclearedNo
5storedData remainsstoredDataStorage5Stored permanently
💡 Function ends, memory variables cleared, storage variables remain with gas cost paid
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
xundefined55clearedcleared
storedData05555
tempundefinedundefined5clearedcleared
Key Moments - 3 Insights
Why does storedData cost gas but temp does not?
Because storedData is saved in storage which is permanent and costly (see step 2 in execution_table), while temp is in memory and temporary (step 3 and cleared at step 4).
What happens to memory variables after function ends?
Memory variables like temp are cleared after the function finishes (step 4), so they do not persist.
Can storage variables be changed without gas cost?
No, changing storage variables costs gas because it writes to the blockchain permanently (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of storedData after step 3?
Acleared
Bundefined
C5
D0
💡 Hint
Check the 'storedData' row after step 3 in variable_tracker.
At which step does the memory variable temp get cleared?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'temp' variable value change in execution_table and variable_tracker.
If storedData was assigned inside memory instead of storage, what would happen to gas cost?
AGas cost would be zero
BGas cost would increase
CGas cost would stay the same
DGas cost would be unpredictable
💡 Hint
Refer to gas cost impact in execution_table for storage vs memory assignments.
Concept Snapshot
Storage vs Memory in blockchain:
- Storage: permanent, costly, saved on blockchain
- Memory: temporary, cheap, used during function execution
- Writing to storage costs gas
- Memory cleared after function ends
- Use storage for data to keep, memory for temporary calculations
Full Transcript
This visual execution shows how blockchain smart contracts handle storage and memory. When a function runs, input variables are received. Assigning a value to a storage variable saves it permanently on the blockchain and costs gas. Assigning to a memory variable uses temporary RAM and costs little or no gas. After the function finishes, memory variables are cleared, but storage variables remain. This distinction is important because storage is expensive and permanent, while memory is cheap and temporary. The execution table traces each step, showing variable values and gas cost impact. Key moments clarify why storage costs gas and memory does not, and what happens to variables after function ends. The quiz tests understanding of variable states and gas costs. The snapshot summarizes the main points for quick review.

Practice

(1/5)
1. Which of the following best describes storage in blockchain smart contracts?
easy
A. Permanent data saved on the blockchain that persists between function calls.
B. Temporary data used only during a function execution and discarded afterward.
C. Data stored off-chain for faster access.
D. Encrypted data that cannot be accessed by the contract.

Solution

  1. Step 1: Understand storage purpose

    Storage holds data permanently on the blockchain, so it remains after function execution.
  2. Step 2: Compare with memory

    Memory is temporary and only lasts during function execution, unlike storage.
  3. Final Answer:

    Permanent data saved on the blockchain that persists between function calls. -> Option A
  4. Quick 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
A. uint memory[] numbers;
B. uint[] storage numbers;
C. memory uint[] numbers;
D. uint[] memory numbers;

Solution

  1. Step 1: Recall Solidity syntax for memory arrays

    In Solidity, the correct syntax is type[] memory variableName; for memory arrays.
  2. Step 2: Check each option

    uint[] memory numbers; matches the correct syntax: uint[] memory numbers;. Others have wrong order or keywords.
  3. Final Answer:

    uint[] memory numbers; -> Option D
  4. Quick 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
A. 0
B. 10
C. 5
D. Compilation error

Solution

  1. Step 1: Analyze tempArray usage

    The function creates a memory array with one element set to 5, then returns that element.
  2. Step 2: Understand storageArray effect

    storageArray.push(10) adds 10 to storage but does not affect the returned value.
  3. Final Answer:

    5 -> Option C
  4. Quick 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
A. Cannot assign storage array directly to memory array.
B. Function must be non-view to modify storage.
C. Missing visibility specifier for storageArray.
D. Memory arrays cannot be returned from functions.

Solution

  1. Step 1: Understand storage to memory assignment

    In Solidity, you cannot directly assign a storage array to a memory array variable.
  2. Step 2: Identify correct copying method

    You must copy elements manually or use a loop to transfer data from storage to memory.
  3. Final Answer:

    Cannot assign storage array directly to memory array. -> Option A
  4. Quick 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
A. Declare the array as memory in the contract state.
B. Copy the storage array to a memory array, modify the memory array, then discard it.
C. Use a storage pointer to the array and modify it temporarily.
D. Modify the storage array directly and revert changes after calculations.

Solution

  1. Step 1: Understand temporary modification needs

    To avoid changing stored data, use a memory copy for temporary changes.
  2. 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.
  3. Step 3: Reject incorrect options

    A declares memory in state (invalid syntax); B risks permanent changes if revert fails; C modifies storage directly.
  4. Final Answer:

    Copy the storage array to a memory array, modify the memory array, then discard it. -> Option B
  5. Quick 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