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).
Jump into concepts and practice - no test required
contract Example {
uint storedData;
function set(uint x) public {
storedData = x; // storage
uint temp = x; // memory
}
}| Step | Action | Variable | Location | Value | Gas Cost Impact |
|---|---|---|---|---|---|
| 1 | Function set called with x=5 | x | Input | 5 | No |
| 2 | Assign x to storedData | storedData | Storage | 5 | Yes (high) |
| 3 | Assign x to temp | temp | Memory | 5 | No (low) |
| 4 | Function ends, temp cleared | temp | Memory | cleared | No |
| 5 | storedData remains | storedData | Storage | 5 | Stored permanently |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| x | undefined | 5 | 5 | cleared | cleared |
| storedData | 0 | 5 | 5 | 5 | 5 |
| temp | undefined | undefined | 5 | cleared | cleared |
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
storage in blockchain smart contracts?memory?type[] memory variableName; for memory arrays.uint[] memory numbers;. Others have wrong order or keywords.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];
}
}pragma solidity ^0.8.0;
contract Example {
uint[] storageArray;
function copyArray() public view returns (uint[] memory) {
uint[] memory tempArray = storageArray;
return tempArray;
}
}