Storage in Structs (Solidity)
Mansoor Butt
1 min read
Lets first look at the code
contract A {
struct SomeStruct{
uint256 val1;
uint16 val2;
bool val3;
}
uint256 a = 3; // slot 0
uint256 b = 0x555 // slot 1
SomeStruct structVar = SomeStruct(0x2345,5,true) // Line 10
function getSlotVal(uint slot) public view returns (bytes32){
assembly{
value := sload(slot)
}
return value;
}
}
In the above code the Line-10 is very interesting the number “0×2345” is being stored on slot-2 and it is “val1” which is a uint256 , which means it will occupy 32 bytes , so the whole slot-2 will be utilised by “0×2345” , However “val2” is uint16 =2 bytes and “val3” is uint8 = 1 byte , hence they will occupy only 3 bytes , so both of them will be placed on slot-3
0
Subscribe to my newsletter
Read articles from Mansoor Butt directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by