> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/yul/memory-safe.md).

# memory-safe

By default solidity code is considered memory safe, but the `inline assembly` blocks need to be declared explictly using `assembly ("memory-safe") { ... }`.&#x20;

The compiler does **NOT** check that it actually is memory safe but turns on the above functionality believing it is actually memory safe on the programmer’s promise. Declaring it as memory safe but not actually being memory safe leads to **undefined behaviours** .

**What is a memory safe assembly block?**&#x20;

TLDR: assembly that respect solidity memory layout. It’s memory safe when the following memory blocks are accessed :

1. Memory allocated by yourself respecting solidity layout i.e **reading from free memory pointer** `0x40` and **updating (incrementing) it correctly after any allocations.**
2. Memory **allocated by Solidity**, e.g. memory within the bounds of a memory array you reference.
3. **The scratch space** between memory offset 0 and 64 mentioned above.
4. Temporary memory that is located *after* the value of the free memory pointer at the beginning of the assembly block, i.e. memory that is “allocated” at the free memory pointer without updating the free memory pointer.
5. The assmebly block that doesn’t have any consecutive allocations. For example, if the assembly block is the last piece of code inside the function, it is safe by default as all memory is wiped after function call finishes.

Link: <https://0xpranay.github.io/solidity-notes/Internals.html>

{% hint style="info" %}
Using memory-safe assembly does not enable any safety checks or restrictions. Quite the opposite.&#x20;

The compiler cannot determine on its own whether the code is memory-safe or not. By using this feature, you’re declaring that it is and signaling that the compiler can do things that would not be safe to do otherwise. If it turns out it’s not memory-safe, bad things can happen.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/yul/memory-safe.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
