# Stack Too Deep

In Solidity, the "stack too deep" error occurs when you have too many local variables in a function. This is a limitation of the Ethereum Virtual Machine (EVM), which Solidity compiles down to. The EVM has a limit on the number of local variables a function can handle, which is typically around 16. This includes explicit local variables, function arguments, and return parameters.

The reason for this limitation is the EVM's design, which uses a stack-based architecture. Each operation in the EVM (including function calls) has a stack to store its data, and this stack has a limited size.

To avoid the "stack too deep" error, you can:

1. **Reduce the number of local variables**: Try to minimize the number of variables in your function. Combine related variables into structs if possible.
2. **Optimize your code**: Sometimes, rearranging the order of variables or breaking a function into smaller functions can help.
3. **Use State Variables**: If some variables can be moved to the contract's state level (outside of the function), this can reduce the number of local variables.
4. **Use Memory Arrays**: For temporary storage of a collection of items, consider using memory arrays.
5. **Inline Functions**: If you're using small helper functions within a larger function, consider inlining these if they are not reused elsewhere.

Be aware that these workarounds might affect gas costs and contract readability, so they should be used judiciously.

<figure><img src="/files/NziFAx4Szm970NM1KXg0" alt=""><figcaption><p><a href="https://twitter.com/PaulRBerg/status/1612043506545033218">https://twitter.com/PaulRBerg/status/1612043506545033218</a></p></figcaption></figure>


---

# Agent Instructions: 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://docs.eridian.xyz/ethereum-dev/solidity-notes/stack-too-deep.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.
