Reference Data types in Solidity

Continuing from where we left ……..

Data locations in solidity (thanks to ChatGPT)
In Solidity, the keywords memory, calldata, and storage define the location where variables are stored and how they are managed in terms of lifetime and mutability. Here’s a detailed explanation of each:

  1. storage

Definition:

storage refers to the persistent storage of the Ethereum blockchain. Variables declared as storage are written to the blockchain and persist between function calls and transactions.

Characteristics:

Persistence: Data stored in storage is permanent and will remain on the blockchain.
Default for State Variables: State variables are by default stored in storage.
Gas Cost: Modifying storage variables is costly in terms of gas.

  1. memory

Definition:

memory refers to temporary storage that exists only for the duration of a function call. Variables declared as memory are not persisted and are discarded after the function execution finishes.

Characteristics:

Temporary: Data stored in memory exists only during the execution of the function.
Default for Local Variables: Local variables within functions default to memory unless specified otherwise.
No Persistence: Memory variables are not stored on the blockchain.
Gas Cost: Less costly than storage for read and write operations.

  1. calldata

Definition:

calldata is a non-modifiable, temporary area where function arguments are stored. It is similar to memory, but specifically used for function parameters that cannot be altered.

Characteristics:

Read-Only: Variables declared as calldata cannot be modified.
Temporary: Exists only for the duration of the external function call.
Cheaper: More gas-efficient for external function calls because it avoids copying data.

The knowledge about these data types is really handy when dealing with reference data types.

Reference data types (Thanks to ChatGPT)
In Solidity, reference types are data types that do not hold the actual data themselves but rather a reference to the data. This means that when you manipulate a reference type, you are working with a reference to the data, rather than a copy of the data. The main reference types in Solidity are arrays, structs, and mappings.
Reference Types in Solidity

Arrays
Structs
Mappings

  1. Arrays

Arrays can be fixed-size or dynamic. Arrays in Solidity can be declared to hold elements of any type.

a) Fixed-Size Arrays

A fixed-size array has a predefined length.

Example:

pragma solidity ^0.8.0; contract Example { uint[3] public fixedArray = [1, 2, 3]; // Array of fixed size 3 function getElement(uint index) public view returns (uint) { require(index < fixedArray.length, "Index out of bounds"); return fixedArray[index]; }
}
Enter fullscreen mode
Exit fullscreen mode

Dynamic Arrays

A dynamic array does not have a predefined length and can be resized.

Example:

pragma solidity ^0.8.0; contract Example { uint[] public dynamicArray; function addElement(uint element) public { dynamicArray.push(element); // Adding an element to the dynamic array } function getElement(uint index) public view returns (uint) { require(index < dynamicArray.length, "Index out of bounds"); return dynamicArray[index]; }
}
Enter fullscreen mode
Exit fullscreen mode
  1. Structs

Structs allow you to create custom data types that group together related data.

Example:

pragma solidity ^0.8.0; contract Example { struct Person { string name; uint age; } Person public person; function setPerson(string memory _name, uint _age) public { person = Person(_name, _age); // Creating a new Person struct } function getPerson() public view returns (string memory, uint) { return (person.name, person.age); }
}
Enter fullscreen mode
Exit fullscreen mode
  1. Mappings

Mappings are key-value stores where keys are mapped to values. Mappings are particularly useful for associative arrays or dictionaries.

Example:


pragma solidity ^0.8.0; contract Example { mapping(uint => string) public idToName; function setName(uint id, string memory name) public { idToName[id] = name; // Setting a value in the mapping } function getName(uint id) public view returns (string memory) { return idToName[id]; }
}
Enter fullscreen mode
Exit fullscreen mode

other than these ref data types we also have a string data type. In Solidity, strings are a reference type used to store arbitrary-length text data. However, strings are a bit unique compared to other reference types like arrays, structs, and mappings due to their handling and limitations within the Ethereum Virtual Machine (EVM).

Characteristics of Strings :

Storage and Memory: Strings can be stored in storage, memory, or calldata, similar to other reference types. The choice of data location affects their behavior and gas cost.
Immutability in Calldata: When a string is passed as a calldata parameter, it is immutable and cannot be modified.
Bytes Representation: Internally, strings are treated as dynamic arrays of bytes (bytes), which are more flexible but less readable.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top