Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Memory allocation in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Memory allocation
Start Transaction
↓
Request Memory Allocation
↓
Check Available Memory
Yes No↓
Allocate Memory
↓
Use Memory
↓
Free Memory
↓
End Transaction
This flow shows how memory is requested, checked, allocated, used, and freed during a blockchain transaction.
Execution Sample
Blockchain / Solidity
memoryPool = 1000
request = 300
if request <= memoryPool:
    memoryPool -= request
    # use memory
    memoryPool += request
This code simulates allocating and freeing memory from a pool during a blockchain operation.
Execution Table
StepmemoryPool BeforerequestCondition (request <= memoryPool)ActionmemoryPool After
11000300TrueAllocate 300 memory700
2700300N/AUse memory (no change)700
3700300N/AFree 300 memory1000
410001100FalseThrow Error: Out of Memory1000
💡 At step 4, request 1100 is greater than available memory 1000, so allocation fails.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
memoryPool100070070010001000
request3003003003001100
Key Moments - 3 Insights
Why does memoryPool decrease at step 1?
Because the requested memory (300) is allocated, so memoryPool reduces from 1000 to 700 as shown in execution_table row 1.
Why does memoryPool return to 1000 at step 3?
At step 3, the allocated memory is freed, adding back 300 to memoryPool, restoring it to 1000 as in execution_table row 3.
What happens if requested memory is more than available?
At step 4, request 1100 is greater than memoryPool 1000, so allocation fails and memoryPool stays unchanged, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is memoryPool after step 1?
A300
B700
C1000
D0
💡 Hint
Check the 'memoryPool After' column in row for step 1.
At which step does the memory get freed back to the pool?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the action 'Free 300 memory' in the execution table.
If request was 1200 instead of 1100 at step 4, what would happen?
AMemoryPool becomes negative
BMemory allocated successfully
CError: Out of Memory
DMemoryPool increases
💡 Hint
Refer to the condition check and action at step 4 in the execution table.
Concept Snapshot
Memory allocation in blockchain:
- Request memory from pool
- Check if enough memory is available
- Allocate memory (reduce pool)
- Use memory
- Free memory (restore pool)
- If request > available, throw error
Full Transcript
This visual execution shows how memory allocation works in blockchain programming. We start with a memory pool of 1000 units. When a request for 300 units comes, we check if the pool has enough memory. Since 300 is less than 1000, we allocate it, reducing the pool to 700. Then we use the memory (no change in pool). After usage, we free the memory, adding 300 back to the pool, restoring it to 1000. If a request exceeds available memory, like 1100, allocation fails and the pool stays the same. This process ensures safe memory management during blockchain transactions.

Practice

(1/5)
1. What is the main purpose of memory in blockchain smart contracts?
easy
A. To temporarily store data during function execution
B. To permanently save user data on the blockchain
C. To allocate gas for contract deployment
D. To encrypt data before storing

Solution

  1. Step 1: Understand memory role in smart contracts

    Memory is used to hold data temporarily while a function runs.
  2. Step 2: Differentiate memory from storage

    Storage saves data permanently on the blockchain, memory does not.
  3. Final Answer:

    To temporarily store data during function execution -> Option A
  4. Quick Check:

    Memory = Temporary data space [OK]
Hint: Memory is temporary, storage is permanent [OK]
Common Mistakes:
  • Confusing memory with storage
  • Thinking memory saves data permanently
  • Assuming memory costs more gas
2. Which of the following is the correct syntax to declare a dynamic array in memory in Solidity?
easy
A. uint[] myArray = memory new uint[](5);
B. uint[] storage myArray = new uint[](5);
C. memory uint[] myArray = new uint[](5);
D. uint[] memory myArray = new uint[](5);

Solution

  1. Step 1: Recall correct memory array declaration syntax

    In Solidity, dynamic memory arrays are declared as type[] memory var = new type[](size);
  2. Step 2: Check each option

    uint[] memory myArray = new uint[](5); matches the correct syntax exactly.
  3. Final Answer:

    uint[] memory myArray = new uint[](5); -> Option D
  4. Quick Check:

    Memory array syntax = uint[] memory myArray = new uint[](5); [OK]
Hint: Use 'memory' keyword before variable name for memory arrays [OK]
Common Mistakes:
  • Using 'storage' instead of 'memory' for temporary arrays
  • Placing 'memory' after variable name incorrectly
  • Omitting 'new' keyword for dynamic arrays
3. What will be the output of this Solidity function snippet?
function test() public pure returns (uint) {
    uint[] memory arr = new uint[](3);
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    return arr[1];
}
medium
A. 10
B. 20
C. 30
D. 0

Solution

  1. Step 1: Analyze array assignments

    Array elements are set as arr[0]=10, arr[1]=20, arr[2]=30.
  2. Step 2: Identify returned value

    The function returns arr[1], which is 20.
  3. Final Answer:

    20 -> Option B
  4. Quick Check:

    arr[1] = 20 [OK]
Hint: Remember array indexing starts at 0 [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Assuming default zero value is returned
  • Misreading array initialization
4. Identify the error in this Solidity function that tries to copy a storage array to memory:
uint[] public data;

function copyArray() public {
    uint[] memory temp = data;
}
medium
A. Cannot assign storage array directly to memory array
B. Missing visibility keyword for storage array
C. Memory arrays cannot be declared inside functions
D. Function must be marked as pure

Solution

  1. Step 1: Understand storage to memory assignment rules

    Direct assignment from storage array to memory array is not allowed in Solidity.
  2. Step 2: Identify correct error

    The code tries to assign data (storage) directly to temp (memory), causing a compilation error.
  3. Final Answer:

    Cannot assign storage array directly to memory array -> Option A
  4. Quick Check:

    Storage to memory direct assignment = Error [OK]
Hint: Copy elements manually from storage to memory [OK]
Common Mistakes:
  • Assuming direct assignment copies array
  • Ignoring visibility requirements
  • Marking function pure incorrectly
5. You want to create a function that returns a new memory array containing only the even numbers from a given storage array. Which approach correctly uses memory allocation?
hard
A. Declare a memory array with length zero, push evens dynamically, then return it
B. Declare a storage array, copy evens, then return it
C. Declare a memory array with max length, copy evens manually, then return a sliced array
D. Declare a memory array with the same length as storage, copy evens, then resize the array

Solution

  1. Step 1: Understand memory array limitations

    Memory arrays in Solidity have fixed size; cannot push dynamically.
  2. Step 2: Choose correct approach for filtering

    Declare a memory array with max length, copy even numbers manually, then return a sliced array with correct size.
  3. Final Answer:

    Declare a memory array with max length, copy evens manually, then return a sliced array -> Option C
  4. Quick Check:

    Memory arrays fixed size = Declare a memory array with max length, copy evens manually, then return a sliced array [OK]
Hint: Memory arrays fixed size; slice after copying [OK]
Common Mistakes:
  • Trying to push to memory arrays
  • Using storage arrays for temporary filtering
  • Not resizing array before returning
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard