0
0
Blockchain / Solidityprogramming~10 mins

Storage layout in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Storage layout
Define variables
Assign storage slots
Store values in slots
Read values from slots
Use values in contract logic
Storage layout shows how variables are assigned to storage slots in a blockchain contract and how values are stored and read.
Execution Sample
Blockchain / Solidity
uint256 a = 5;
uint256 b = 10;

// a stored at slot 0
// b stored at slot 1

function getA() public view returns (uint256) {
  return a;
}
This code stores two numbers in separate storage slots and reads one back.
Execution Table
StepActionStorage SlotValue StoredVariableResult/Output
1Define variable aslot 0uninitializedanone
2Define variable bslot 1uninitializedbnone
3Assign a = 5slot 05anone
4Assign b = 10slot 110bnone
5Call getA()slot 05areturns 5
6End---Execution complete
💡 Execution stops after returning value of a from slot 0
Variable Tracker
VariableStartAfter Step 3After Step 4Final
auninitialized555
buninitializeduninitialized1010
Key Moments - 2 Insights
Why does variable 'a' use slot 0 and 'b' use slot 1?
Variables are assigned storage slots in the order they are declared. 'a' is first, so slot 0; 'b' is second, so slot 1, as shown in steps 1 and 2 of the execution_table.
What happens if we read a variable before assigning a value?
Reading before assignment returns default uninitialized value (usually 0). In the table, before step 3, 'a' is uninitialized.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value is stored in slot 1 after step 4?
A10
B5
Cuninitialized
D0
💡 Hint
Check the 'Storage Slot' and 'Value Stored' columns at step 4 in execution_table.
At which step does variable 'a' get its value assigned?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where 'Assign a = 5' happens.
If we add a new variable 'c' after 'b', which storage slot will it use?
Aslot 0
Bslot 1
Cslot 2
Dslot 3
💡 Hint
Variables are assigned slots in order; 'a' is slot 0, 'b' slot 1, so next is slot 2.
Concept Snapshot
Storage layout assigns each variable a unique storage slot.
Variables declared first get lower slot numbers.
Values are stored and read from these slots.
Uninitialized slots hold default values.
Reading a variable returns the value in its slot.
Full Transcript
Storage layout in blockchain contracts means each variable is stored in a specific slot in contract storage. When you declare variables, the first one gets slot 0, the second slot 1, and so on. Assigning a value stores it in that slot. Reading a variable fetches the value from its slot. If a variable is not assigned yet, its slot holds a default value, usually zero. This layout helps the contract know where to find each variable's data.