What are data locations in Solidity and their types?

Data locations

Data locations in solidity describe how and where data is kept or accessible on the Ethereum blockchain. The Storage, memory, and calldata are the three significant data places offered by Solidity. For the creation of smart contracts to be effective and safe, it is crucial to comprehend these data locations.

Storage

  • Data is stored in persistent storage in the blockchains.

  • Used for state variables. Those variables that are declared outside of functions are known as state variables.

  • State variables are stored in contract storage and retain their values between function calls and transactions.

pragma solidity ^0.5.0;
contract StorageExample {
uint256 public storedData;
function setStoredData(uint256 newValue) public {
storedData = newValue;
}
function getStoredData() public view returns (uint256) {
return storedData;
}
}

Explanation

In line 4, we create storedData the state variable stored in contract storage. Changes made in storedData are permanent and affected by the blockchain state between function calls and transactions. In lines 6 and 10, we created a getter and setter to set the value of storedData the variable.

Memory

  • Variables are in memory and are present throughout the function call; therefore, they are transitory and will disappear when the function has completed running.

  • Used for local variables, function arguments, and return values.

pragma solidity ^0.5.0;
contract MemoryExample {
function mathoperation(uint256 value1, uint256 value2) public pure returns (uint256) {
uint256 answer;
answer = a + b; // Temporary value stored in memory
return answer;
}
}

Explanation

In line 4, we create the function name the mathoperation that takes two variable value1 and value2 and add them and store them in the variable answer.The answer variable is stored in memory within the mathoperation function. Memory is used to store temporary data during function execution.

Calldata

  • Read-only data location is used for function arguments.

  • Immutable data location.

  • Accessing data from calldata is cheaper than storage.

pragma solidity ^0.5.0;
contract CalldataExample {
function joinTwoStrings(string calldata string1, string calldata string2) public pure returns (string memory) {
bytes memory result = bytes(string1); // Calldata data used for reading
result = abi.encodePacked(result, bytes(string2)); // Concatenate strings in memory
return string(result);
}
}

Explanation

In line 5, we create the joinTwoString function that takes two arguments string1 and string2 having calldata . The calldata is used for input data that is read-only and immutable. The strings are concatenated in memory before returning the result.

Behavior of data locations

  • Assignments between the memory and storage always produce a distinct copy.

  • Assignments originating from the storage to a local storage variable solely establish a reference.

  • Assignments from one memory location to another memory location only creates references. Consequently, modifications made to one memory variable are also observable in all other memory variables that point to the same data.

  • Every other assignment to storage invariably generates distinct, separate copies.

Note: It's essential to comprehend the proper use of data locations in order to optimize costs and guarantee that smart contracts on the Ethereum blockchain operate as intended.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved