Storing Static Type in Solidity's Storage

Mansoor ButtMansoor Butt
1 min read

Let’s first have a look at the Code

contract Demo{
    uint256 a 1234 // slot 0
    uint8 b = 0x12; // slot 1
    uint8[6] c = [1,2,3,4,5,6]; // line 4 (slot 2)
    uint256[2] d = [10,20] // line 5 slot(3 & 4)

function getSlotvalue(uint slot) public view returns(bytes32)
{
    bytes32 value;
    assembly{
        value := sload(slot)
    }
   return value
}
}

In the above example , We try to explain how Static Storage works in Solidity ? in Line 4 , We have a fixed-sized array “c” with a length of 6 and note that it’s a “uint8” array ,which means for every element in the array we have assigned 8 bits or 1 byte

So the total number of elements in the array c are 6 , hence the array will occupy 6 bytes out of 32 bytes , so all the elements will be placed on Slot-2

However in line 5 the array “ d ” has only 2 elements but it is allocating 256 bits or 32 bytes for each element , which means each Element will occupy the an entire slot , hence number 10 will take slot-3 and consequently 20 will take slot-4 .

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

Mansoor Butt
Mansoor Butt