Memory allocation is how a program sets aside space to store data while it runs. It helps the program remember information it needs to use or change.
Memory allocation in Blockchain / Solidity
In blockchain smart contracts (like Solidity), memory allocation is often implicit, but you can specify data location: function example() public { uint[] memory tempArray = new uint[](5); // allocate memory for 5 uints }
Memory in blockchain smart contracts is temporary and cleared after function execution.
Storage is permanent and costs more gas; memory is cheaper but temporary.
uint[] memory numbers = new uint[](3);string memory name = "Alice";mapping(address => uint) balances; // stored permanently in storageThis smart contract function creates a temporary array in memory, fills it with numbers, and returns it. The array only exists while the function runs.
pragma solidity ^0.8.0; contract MemoryExample { function createArray() public pure returns (uint[] memory) { uint[] memory tempArray = new uint[](3); tempArray[0] = 10; tempArray[1] = 20; tempArray[2] = 30; return tempArray; } }
Memory is cleared after the function finishes, so data stored there is temporary.
Use memory for temporary variables and storage for data you want to keep.
Allocating too much memory can increase gas costs, so be efficient.
Memory allocation sets aside space to hold data temporarily during contract execution.
Memory is cheaper but temporary; storage is permanent but costs more.
Use memory for temporary data like function variables and arrays.
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
