Structs

Structs in memory

  • Allocating a struct to memory:

struct Point {
        uint256 x;
        uint256 y;
    }

function memPointer() external {
        bytes32 x40;
        assembly {
            x40 := mload(0x40) // check contents of the free memory pointer 
        }
        emit MemoryPointer(x40); // returns 0x80 -> 128 
        Point memory p = Point({x: 1, y: 2}); // allocate a struct in memory 

        assembly {
            x40 := mload(0x40) 
        }
        emit MemoryPointer(x40); // returns 0xc0 -> 192 
    }

// 0xc0 - 0x80 = 64 bytes -> two uint256s (size of the struct) 
  • Seeing msize in action:

Last updated