Memory allocation in Blockchain / Solidity - Time & Space Complexity
When working with blockchain, memory allocation affects how fast your code runs as it handles data.
We want to know how the time to allocate memory grows when the data size changes.
Analyze the time complexity of the following code snippet.
function allocateMemory(uint size) public {
bytes memory data = new bytes(size);
for (uint i = 0; i < size; i++) {
data[i] = 0x00;
}
}
This code creates a new memory array of a given size and fills it with zeros.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that sets each byte to zero.
- How many times: It runs exactly once for each element in the array, so 'size' times.
As the size of the array grows, the number of steps to fill it grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps to fill |
| 100 | 100 steps to fill |
| 1000 | 1000 steps to fill |
Pattern observation: The work grows directly with the size; doubling the size doubles the steps.
Time Complexity: O(n)
This means the time to allocate and fill memory grows in a straight line with the size of the data.
[X] Wrong: "Memory allocation happens instantly no matter the size."
[OK] Correct: Actually, filling memory takes time for each element, so bigger sizes take longer.
Understanding how memory allocation time grows helps you write efficient blockchain code that handles data well.
"What if we removed the loop that fills the array? How would the time complexity change?"
Practice
memory in blockchain smart contracts?Solution
Step 1: Understand memory role in smart contracts
Memory is used to hold data temporarily while a function runs.Step 2: Differentiate memory from storage
Storage saves data permanently on the blockchain, memory does not.Final Answer:
To temporarily store data during function execution -> Option AQuick Check:
Memory = Temporary data space [OK]
- Confusing memory with storage
- Thinking memory saves data permanently
- Assuming memory costs more gas
Solution
Step 1: Recall correct memory array declaration syntax
In Solidity, dynamic memory arrays are declared astype[] memory var = new type[](size);Step 2: Check each option
uint[] memory myArray = new uint[](5); matches the correct syntax exactly.Final Answer:
uint[] memory myArray = new uint[](5); -> Option DQuick Check:
Memory array syntax = uint[] memory myArray = new uint[](5); [OK]
- Using 'storage' instead of 'memory' for temporary arrays
- Placing 'memory' after variable name incorrectly
- Omitting 'new' keyword for dynamic arrays
function test() public pure returns (uint) {
uint[] memory arr = new uint[](3);
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
return arr[1];
}Solution
Step 1: Analyze array assignments
Array elements are set as arr[0]=10, arr[1]=20, arr[2]=30.Step 2: Identify returned value
The function returns arr[1], which is 20.Final Answer:
20 -> Option BQuick Check:
arr[1] = 20 [OK]
- Confusing index 1 with index 0
- Assuming default zero value is returned
- Misreading array initialization
uint[] public data;
function copyArray() public {
uint[] memory temp = data;
}Solution
Step 1: Understand storage to memory assignment rules
Direct assignment from storage array to memory array is not allowed in Solidity.Step 2: Identify correct error
The code tries to assigndata(storage) directly totemp(memory), causing a compilation error.Final Answer:
Cannot assign storage array directly to memory array -> Option AQuick Check:
Storage to memory direct assignment = Error [OK]
- Assuming direct assignment copies array
- Ignoring visibility requirements
- Marking function pure incorrectly
Solution
Step 1: Understand memory array limitations
Memory arrays in Solidity have fixed size; cannot push dynamically.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.Final Answer:
Declare a memory array with max length, copy evens manually, then return a sliced array -> Option CQuick Check:
Memory arrays fixed size = Declare a memory array with max length, copy evens manually, then return a sliced array [OK]
- Trying to push to memory arrays
- Using storage arrays for temporary filtering
- Not resizing array before returning
Ready to prove it?
Pass the test and this pattern moves unknown
