Storage vs. Memory

Learn about the differences between storage and memory in Solidity.

Storage and memory

We already know that reference data types don’t hold value directly; instead, they store the location of the data. This way, two different variables can point to the same location, where any change to one variable can affect the other one. When defining reference data type variables, we must specify the data location using storage or memory.

In Solidity, storage refers to a permanent location for the defined data. Data stored in storage is permanent and remains between function calls. However, memory refers to a temporary location for storing the associated data, which is immediately cleared once the function enclosing the variable is called.

Let’s look at the code below:

Get hands-on with 1200+ tech skills courses.