Understand Types Of Variable Location

Published in
5 min readJun 16, 2023


Variables can be stored in a variety of ways using Solidity, the programming language used to create smart contracts on the Ethereum blockchain. These choices control how and where data is kept while a contract is being executed. For the construction of smart contracts to be effective and secure, it is essential to comprehend these storage places.

To quickly review, The value type, memory type, user-defined type, and type aliases are the four primary sorts of variables in Solidity that we previously covered. These variable types and their traits were briefly described in the previous blog. To manage and manipulate data in Solidity contracts efficiently, one must have a solid understanding of these types. Let’s now examine the various storage places offered by Solidity in more detail.

There are 3 places where solidity variables can be found: the storage, memory and local variable (stack). Let’s go over each one in more depth.


Storage can be thought of as a mapping of 32-byte slots to 32-byte values. When something is written to storage within a smart contract, it becomes persistent even after the transaction is completed. This means that the value stored in storage remains intact until explicitly deleted.

One important characteristic of storage is that uninitiated slots, or slots that have not been assigned a value, return zero when read. This can be useful for checking the initialisation status of a particular slot or for distinguishing between initialised and uninitialised data. Storage variables are index based.

The storage is where all the contract state variables reside. Every contract has its own storage. Variables stored in storage persist between function calls. However, the storage is quite expensive to use.


Another data location utilised by Solidity to hold transient data during contract execution is memory. Memory is transient and does not endure past the end of a single function call, in contrast to storage, which holds onto its data throughout function calls and even after a contract has ended.

Memory is consumed when a function is called and temporary storage is needed for variables, such as arrays or sophisticated data structures. Memory is explicitly created and deallocated within the bounds of the function, so its contents are not preserved or accessible outside of that particular function call.

When working with dynamic arrays or carrying out intricate calculations within a function, memory is particularly helpful. It enables effective data processing and calculation without the requirement to keep the interim results for later reference.

Solidity programmers may minimise gas consumption and guarantee proper memory management in their smart contracts by making good use of memory. It offers a versatile and effective location for temporary data storage during contract execution, enabling the completion of intricate calculations and data manipulations inside the bounds of a single function call.


In the Solidity programming language, the stack plays a crucial role in the execution of a smart contract. Data used by functions to carry out contract execution are stored there. Opcodes interact with the stack by reading and writing data to it. Stack is a non-persistent data maintained by EVM (Ethereum Virtual Machine).

It is important to note that the stack has a limited capacity. In the context of Solidity, the maximum stack size is 1024 elements. To prevent any unforeseen repercussions or problems during contract execution, developers must carefully analyse the order and timing of data pushed and popped from the stack.

When necessary, Solidity offers keywords like storage, memory, and calldata that allow the precise specification of the variable placement. In addition, there is a unique read-only variable called calldata that is used to hold the function arguments. When accessing function arguments in calls to external functions, it is used. Solidity also uses the following four important keywords: calldata, returndata, constants, and immutable. Let’s also comprehend these crucial phrases.


In Solidity, the input data for a function call is stored in a particular data location called the calldata. The smart contract can only access and read the data that was supplied to it during the function call because it is read-only. It is frequently employed for communicating with other smart contracts via calls to external functions. The calldata is a cost-free method of retrieving the function arguments that saves on storage space.


Another unique data location used to hold the return data from an external function call is called returns. The Solidity return and revert opcodes are used to populate it. The smart contract has the ability to both read the data returned by the external call and populate the returndata with a value to be returned. This enables retrieval of outcomes from calls to external function calls as well as communication between smart contracts.


Solidity uses a specific class of variables called constants that are computed at compile time. Their values are directly encoded into the bytecode of the contract where they are used after being assessed and given a value during the compilation process. Only fundamental data types, including integers, booleans, and addresses, can be assigned to constants.


On the other hand, immutable are comparable to constants but have a different evaluation time. Variables that are computed and given a value at contract deployment are known as immutable. Their values are kept in the contract bytecode and they are assessed during contract deployment. Complex types like structs, arrays, and mappings can be immutable.

In conclusion, understanding the different types of variable location in Solidity is crucial for efficient and secure smart contract development. Solidity provides three main storage places: storage, memory, and stack.Additionally, Solidity uses two important keywords: constants and immutable. Constants are variables whose values are computed at compile time and directly encoded into the contract bytecode. Immutable variables are computed and given a value at contract deployment and can include complex types like structs, arrays, and mappings.

Understanding these concepts and utilizing them appropriately in smart contracts can help optimize gas consumption, ensure proper memory management, and enable effective data manipulation and calculations. Please clap 👏 if you find something to be informative.



Zinzuvadiya Meet

Computer scientist with a passion for blockchain. Interest in cryptography and basic Solidity skills. familiarity with tools like web3.js, truffle & Ethereum.