DEV Community

Cover image for Ethereum-Solidity Quiz Q10: What is the Free Memory Pointer?
MihaiHng
MihaiHng

Posted on

Ethereum-Solidity Quiz Q10: What is the Free Memory Pointer?

The Free Memory Pointer is a special pointer in the EVM that keeps track of where free(unused) memory starts, to avoid overwriting previously stored data.

The free memory pointer is stored at memory location 0x40(the first 64 bytes of memory are reserved). It holds the address of the next available memory location where you can safely write data without overwriting existing data.

When memory is used:

  1. Read the current free memory pointer at 0x40
  2. Write your data there
  3. Update the pointer to point to a new free memory location

Solidity handles this automatically for you. When you declare variables in functions or use dynamic arrays, the compiler manages the free memory pointer behind the scenes. However, for advanced patterns, you can access it directly in assembly. Not all languages share this feature(ex. Vyper and Huff).

Important note:
In pure view/pure functions, memory is temporary and freed after the function call, so the free memory pointer resets. In external/public functions that return data, the memory needs to persist until the data is returned.

Top comments (0)