Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Data Location
  • Stack Data Location
  1. STEP 3

25: Data Location

Data Location

Data Location is a concept within Solidity, which refers to the memory or storage locations where data is stored. Knowing where data is located can help you write more efficient smart contracts.

Memory

Memory is a volatile location where data is stored temporarily while a function is being executed. All data stored in memory is automatically cleared once the function ends.

  • Example:

function addTwo(uint a, uint b) public pure returns (uint) {    
    uint c = a + b; // c is stored in memory    
    return c;
}

Storage

Storage is a persistent location where data is stored on the blockchain. Data stored in storage is accessible and modifiable until a contract is destroyed or removed from the blockchain.

  • Example:

uint public num; // num is stored in storage  
function setNum(uint a) public {    
    num = a; // set num in storage
}

Stack Data Location

In Solidity, data location refers to the place where the data is stored. It can be either storage, memory, or calldata.

Storage is the permanent storage area for a contract’s data. All data stored in storage will persist until it is explicitly deleted.

Memory is a temporary storage area. It is volatile, meaning that the data is lost when the function call ends.

Calldata is the read-only area where the arguments passed to the contract are stored.

Examples:

  • Storage

contract MyContract {    
    uint256 public value;        
    function setValue(uint256 _value) public {        
        value = _value; // value is stored in storage    
    }
}
  • Memory

contract MyContract {    
    uint256 public value;        
    function setValue(uint256 _value) public {        
        uint256 tempValue = _value; // tempValue is stored in memory        
        value = tempValue;    
    }
}
  • Calldata

contract MyContract {    
    uint256 public value;        
    function setValue(uint256 _value) public {        
        value = _value; // _value is stored in calldata    
    }
}

That's it for the lesson 25! In the next lesson, Inheritance

Previous24: StructsNext26: Inheritance

Last updated 2 years ago