If you are finding it difficult to comprehend either storage, memory, or calldata, you are not alone.If you are finding it difficult to comprehend either storage, memory, or calldata, you are not alone.

Memory, Calldata, and Storage in Solidity: Understanding the Differences

:::warning DISCLAIMER:

  • The code in this article is for educational purposes only.

:::

If you are finding it difficult to comprehend either storage, memory, or calldata, you are not alone. This is an area most beginner developers struggle to grasp, and even some experienced Solidity developers still don’t fully understand.

What are these ‘special words’? They are words that specify the main data locations in a Solidity smart contract. Data locations in Solidity describe where data can be stored and how it can be accessed on the Ethereum blockchain. Other data locations include the following:

  • Stack
  • Code
  • Logs

In this article, you will learn the differences between each main data location option and where to use each.

Firstly, what is storage?

Storage

Storage is the data location that holds a smart contract’s state variables. A state variable is data that lives permanently on the blockchain. Each smart contract has its own storage space (an array of 2^256 32-byte slots), and state variables are automatically assigned to storage.

The code snippet below shows a simple implementation of a state variable stored in storage, as well as its getter and setter functions: \n

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; contract Storage {     /////////////////////////////     // STATE VARIABLES //////////     /////////////////////////////     uint256 public s_storedData; // s_storedData is a storage variable, s_ denotes a storage variable     /////////////////////////////     // SETTER FUNCTION //////////     /////////////////////////////     function set(uint256 x) public {         s_storedData = x; // This value is saved permanently on-chain     }     /////////////////////////////     // GETTER FUNCTION //////////     /////////////////////////////     function getStoredData() public view returns(uint256) {         return s_storedData;     } } 

In the code above, the  s_storedData remains on the blockchain after the set() function runs.

More importantly, storage is one of the major data location options that isn’t explicitly specified. Any variable declared outside any function is implicitly converted to a storage variable.

Use-case

The Bank contract above acts as a simple bank, as the name implies. The balancesmapping is stored in storage, which means it remembers values across function calls.

For instance, if Alice calls deposit(100), her balance is saved in storage in the balances mapping. Conversely, if she calls withdraw(60), she receives 60 and the mapping updates to 40.

Memory

Unlike Storage, Memory is a temporary, function-scope data location. Function-scope means, variables exist only during a function call, not at the contract-level, and clears afterwards. Additionally, memory allows for read-write access; this means variables are modified within a function. Solidity allocates memory to variables defined inside a function(local variables) or parameters marked memory.

To demonstrate:

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; contract Memory {     function multiply(uint256 a, uint256 b) public pure returns (uint256) {         uint256 result = a * b; // 'result' is stored in memory         return result; // 'result' does NOT persist after the function ends     } } 

In the code above, Solidity implicitly stores result in memory, which does not persist after the function execution.

Use-case

The code below concatenates two string inputs, string memory first, string memory second exist while the function combineStrings runs.

A note on function parameters

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; contract Memory {     function multiply(uint256 memory a, uint256 memory b) public pure returns (uint256) {         uint256 result = a * b;         return result;      } } 

For function parameters, you can’t specify the memorykeyword for uint, bool, addressand enum variables as they are directly stored on the contract’s stack, no explicit keyword.

Whereas for reference types like strings, bytes, arrays, structs, and mapping, you will have to specify or defaults to memory for internal and private functions and calldata for external and public functions(more on this below👇).

Calldata

Lightly touched on in the previous section, calldata is another temporary data location, but it’s reserved for external function parameters.  Also, unlike memory, calldata cannot be modified. To explain what this means, let’s take a look at the code below:

\

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; contract Example {     function tryChangeCalldata(uint[] calldata nums) external pure returns (uint[] calldata) {         nums[0] = 999; // ❌ ERROR — "calldata is read-only"         return nums;     } } 

The error message above shows that calldata is read-only. To modify calldata variables, they must first be loaded into memory.

Take the adjusted code below now modified .The code below works because the it loads nums variable to memory before modifying it in the if block.

\

// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; contract Example { &nbsp; &nbsp; function tryChangeCalldata(uint[] calldata nums)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; external&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pure&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returns (uint[] memory)&nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; // Copy from calldata to memory &nbsp; &nbsp; &nbsp; &nbsp; uint[] memory numsCopy = new uint[](nums.length); &nbsp; &nbsp; &nbsp; &nbsp; for (uint i = 0; i < nums.length; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numsCopy[i] = nums[i]; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; // Now modify the copy &nbsp; &nbsp; &nbsp; &nbsp; if (numsCopy.length > 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numsCopy[0] = 999; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; return numsCopy; &nbsp; &nbsp; } } 

\

Why Data Locations Matter

Using appropriate data locations matters as they directly affect how your contracts store, access, and pay for data. The differences can have a large impact on cost, behaviour, and security of your smart contracts. Here’s how:

Gas Costs

  • Writing data to Storage is the most expensive.
  • The gas costs in Memory is more moderate than storage.
  • Calldata is the cheapest data location, i.e., writing to calldata costs the least amount of gas.

Persistence and Temporariness

  • Any data written to Storage lives onchain, meaning the data is permanent.
  • Memory only persists during the function’s lifecycle.
  • Calldata acts like memory in handling data, only it can’t be changed.

Mutability

  • Storage data can be modified.
  • Similar to Storage, Memory can be modified but only within the context of a function.
  • Calldata is read-only.

Safety

  • Storage can be a risky choice of location because mistakes or hacks can permanently change the blockchain’s state.
  • Memory is a safer option since changes are temporary.
  • Calldata is the safest for passing input for external functions.

Wrapping Up

That’s it for this piece, as previously stated, using the appropriate data locations is integral to the functionality of your contracts. I implore you to research further about the other types of data locations mentioned in the introduction of this article.

Happy Hacking!!

Market Opportunity
Notcoin Logo
Notcoin Price(NOT)
$0.0004977
$0.0004977$0.0004977
-4.76%
USD
Notcoin (NOT) Live Price Chart
Disclaimer: The articles reposted on this site are sourced from public platforms and are provided for informational purposes only. They do not necessarily reflect the views of MEXC. All rights remain with the original authors. If you believe any content infringes on third-party rights, please contact service@support.mexc.com for removal. MEXC makes no guarantees regarding the accuracy, completeness, or timeliness of the content and is not responsible for any actions taken based on the information provided. The content does not constitute financial, legal, or other professional advice, nor should it be considered a recommendation or endorsement by MEXC.

You May Also Like

Japan-Based Bitcoin Treasury Company Metaplanet Completes $1.4 Billion IPO! Will It Buy Bitcoin? Here Are the Details

Japan-Based Bitcoin Treasury Company Metaplanet Completes $1.4 Billion IPO! Will It Buy Bitcoin? Here Are the Details

The post Japan-Based Bitcoin Treasury Company Metaplanet Completes $1.4 Billion IPO! Will It Buy Bitcoin? Here Are the Details appeared on BitcoinEthereumNews.com. Japan-based Bitcoin treasury company Metaplanet announced today that it has successfully completed its public offering process. Metaplanet Grows Bitcoin Treasury with $1.4 Billion IPO The company’s CEO, Simon Gerovich, stated in a post on the X platform that a large number of institutional investors participated in the process. Among the investors, mutual funds, sovereign wealth funds, and hedge funds were notable. According to Gerovich, approximately 100 institutional investors participated in roadshows held prior to the IPO. Ultimately, over 70 investors participated in Metaplanet’s capital raising. Previously disclosed information indicated that the company had raised approximately $1.4 billion through the IPO. This funding will accelerate Metaplanet’s growth plans and, in particular, allow the company to increase its balance sheet Bitcoin holdings. Gerovich emphasized that this step will propel Metaplanet to its next stage of development and strengthen the company’s global Bitcoin strategy. Metaplanet has recently become one of the leading companies in Japan in promoting digital asset adoption. The company has previously stated that it views Bitcoin as a long-term store of value. This large-scale IPO is considered a significant step in not only strengthening Metaplanet’s capital but also consolidating Japan’s role in the global crypto finance market. *This is not investment advice. Follow our Telegram and Twitter account now for exclusive news, analytics and on-chain data! Source: https://en.bitcoinsistemi.com/japan-based-bitcoin-treasury-company-metaplanet-completes-1-4-billion-ipo-will-it-buy-bitcoin-here-are-the-details/
Share
BitcoinEthereumNews2025/09/18 08:42
CME Group to Launch Solana and XRP Futures Options

CME Group to Launch Solana and XRP Futures Options

The post CME Group to Launch Solana and XRP Futures Options appeared on BitcoinEthereumNews.com. An announcement was made by CME Group, the largest derivatives exchanger worldwide, revealed that it would introduce options for Solana and XRP futures. It is the latest addition to CME crypto derivatives as institutions and retail investors increase their demand for Solana and XRP. CME Expands Crypto Offerings With Solana and XRP Options Launch According to a press release, the launch is scheduled for October 13, 2025, pending regulatory approval. The new products will allow traders to access options on Solana, Micro Solana, XRP, and Micro XRP futures. Expiries will be offered on business days on a monthly, and quarterly basis to provide more flexibility to market players. CME Group said the contracts are designed to meet demand from institutions, hedge funds, and active retail traders. According to Giovanni Vicioso, the launch reflects high liquidity in Solana and XRP futures. Vicioso is the Global Head of Cryptocurrency Products for the CME Group. He noted that the new contracts will provide additional tools for risk management and exposure strategies. Recently, CME XRP futures registered record open interest amid ETF approval optimism, reinforcing confidence in contract demand. Cumberland, one of the leading liquidity providers, welcomed the development and said it highlights the shift beyond Bitcoin and Ethereum. FalconX, another trading firm, added that rising digital asset treasuries are increasing the need for hedging tools on alternative tokens like Solana and XRP. High Record Trading Volumes Demand Solana and XRP Futures Solana futures and XRP continue to gain popularity since their launch earlier this year. According to CME official records, many have bought and sold more than 540,000 Solana futures contracts since March. A value that amounts to over $22 billion dollars. Solana contracts hit a record 9,000 contracts in August, worth $437 million. Open interest also set a record at 12,500 contracts.…
Share
BitcoinEthereumNews2025/09/18 01:39
Why the Testing Method Developers Prefer Is Rarely Ever the One That Finds the Most Bugs

Why the Testing Method Developers Prefer Is Rarely Ever the One That Finds the Most Bugs

A replicated controlled study confirms that developers’ perceptions, preferences, and opinions about software testing techniques do not reliably predict actual
Share
Hackernoon2025/12/18 05:00