Solidity Reference Types

Solidity Reference Types

It is possible to modify the values of reference types by using a variety of different names. This is in contrast to value types, where each time a value type variable is used, you get an independent duplicate of that variable. This necessitates the fact that reference types must be handled with greater care than value types. At the moment, reference types include structs, arrays, and maps, among other things.

If you use a reference type, you must always explicitly specify the data area in which the type is stored: memory (whose lifetime is limited to the duration of an external function call), storage (the location where the state variables are stored, whose lifetime is limited to the duration of a contract), or calldata (the location where the type is stored) (special data location that contains the function arguments).

The automated copy process will always be triggered by an assignment or type conversion that changes the data location, whereas assignments inside the same data location will only copy in some situations, depending on the storage type.

Data Locations:

Every reference type contains an additional descriptor, known as the "data location," that indicates where the data is kept. Memory, storage, and calldata are the three types of data storage available. Unlike memory, calldata is a static area that cannot be modified and is not persistent. It is used to store function arguments and acts in most ways like memory.

Storage vs Memory vs Calldata:

When designing smart contracts in Solidity, it is important to be aware of how your variables and data are handled by the Ethereum Virtual Machine (EVM). The decisions you make will have an impact on a variety of factors, including gas costs — whether to call your functions or deploy your contract — as well as the structure of your storage facility.

Given that every piece of block space in Ethereum is extremely valuable (hence the high price of Eth, which has since dropped), the efficiency of your code and the resulting contract, which will hopefully be invoked on a regular basis, is greatly influenced. The gas used by every function call adds up; every amount of savings adds up when summed across the potential lifetime of all function invocations in a given time period.

For more content, follow me on - https://linktr.ee/shlokkumar2303

Did you find this article valuable?

Support Shlok Kumar by becoming a sponsor. Any amount is appreciated!