> 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/compendium/local-variables-storage-v-memory.md).

# Local Variables (Storage v Memory)

Only complex data types (arrays and structs) **default** to **storage inside functions**, while all others default to memory.

{% hint style="info" %}
Storage is a key/value store where keys and values are both 32 bytes. Memory is a byte-array. Memory starts off zero-size, but can be expanded in 32-byte chunks by simply accessing or storing memory at indices greater than its current size.\
[\
https://ethereum.stackexchange.com/questions/1232/difference-between-memory-and-storage](https://ethereum.stackexchange.com/questions/1232/difference-between-memory-and-storage)
{% endhint %}

A consequence of this design difference is that **storage is dynamic** and memory is not.&#x20;

Because arrays and structs are complex and could be of variable length, they are defaulted to storage, which has this key:value behaviour.&#x20;

Simpler variables like bool, uint, etc are not variable in length, and are therefore defaulted to memory, which is [cheaper than storage](https://ethereum.stackexchange.com/questions/23720/usage-of-memory-storage-and-stack-areas-in-evm/23722). So, think of the design choice as a compromise between flexibility and cost.

It is possible to [create memory arrays](https://solidity.readthedocs.io/en/develop/types.html#arrays), but once created they cannot be resized (check out the Allocating Memory Arrays section).

{% hint style="info" %}
For value types (booleans, integers, addresses ...) it's memory. For complex types (arrays, structs, maps) the default location depends on the context and can be [overridden](http://solidity.readthedocs.io/en/develop/types.html#data-location) by `memory` and `storage` keywords.
{% endhint %}

## [Do intermediate memory variables cost gas?](https://ethereum.stackexchange.com/questions/60042/do-intermediate-memory-variables-cost-gas)

```solidity
pragma solidity ^0.4.24;
contract A {
    uint t;

    function run() public returns(uint){
        uint startTime = 9;
        uint allowedTime = 7;

        uint v = startTime + allowedTime; // second test without v
        t = now + v;
        return t;
    }
}
```

#### Results

```
with v
  deploy 95237 gas
  func 41470 gas

without v
  deploy 95237 gas
  func 41470 gas
```

* Compiling with the optimizer leads to the same bytecode and gas cost.
* If the optimizer is not used, the compiler would produce more bytecode, and the gas cost would be higher.

#### Links&#x20;

* <https://medium.com/coinmonks/solidity-bits-storage-vs-memory-a54a650ea4ff>
* <https://solidity-by-example.org/data-locations/>


---

# 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/compendium/local-variables-storage-v-memory.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.
