Storage, Memory and Calldata | Solidity 0.8

Smart Contract Programmer · 31K views · 2021 · 6:15

Open on YouTube

Programming language designSoftware practice, open source & careerstutorial

Why Ethereum Stack Exchange recommends it

Ethereum Stack Exchange answerers point people to it when explaining why Solidity requires a data location and how storage, memory and calldata differ.

Recommended by 2 people across 2 different months, 2022 to 2023.

What it is

A short Solidity 0.8 tutorial from the Smart Contract Programmer channel explaining the three data locations for dynamic types: storage, memory and calldata. It walks through function inputs and outputs, declaring in-memory arrays, and when calldata applies, with examples in Remix and Solidity by Example.

What people said

Recommends Munkh-Od Ganzorigt · 2022-03 · Ethereum Stack Exchange · on “Data location must be "memory" or "calldata" for parameter in function, but none was given”

You have to set data location type to function parameters & returns to establish where the data would be during the life time of the function you would be calling. "memory" & "calldata" & "storage", would establish where the function parameter data would be store and where the data is placed so the function return can return these data. Watch these videos and example code to understand better :> [this video] [LINK]

Recommends Javier Marchetti · 2022-09 · Ethereum Stack Exchange · on “Can't update the value of a state variable in Solidity!”

The reason is because you are using memory when you asign the value of your mapping to fd here: FactoryDetail[] memory fd =IdToFactory[_supplyChainId]; that means that a copy of your value is created in memory and when you modify fd what is modified is that copy If you need to modify the actual value you should use: storage FactoryDetail[] storage fd =IdToFactory[_supplyChainId]; take a look at this video [this video] to learn more you can read more about storage and memory here [LINK]

Cites Whisrambol · 2022-03 · Ethereum Stack Exchange · on “Can calldata be used in every function visibility?”

Thank you very much for answering me, but when I watched this video( [this video] ), he called calldata from within the contract and used it. I still have question marks about my first question.

Cites Rohan Nero · 2023-09 · Ethereum Stack Exchange · on “Optimizing Gas Costs in Solidity Smart Contracts: Best Practices?”

I think my answer, in one way or another, answers all five of your specific questions. There are a handful of commonly known practices to avoid to keep gas costs low and/or prevent transactions from running out of gas. Here are three examples: Developers usually try to stray away from using and looping through dynamic arrays since the transaction will fail with out-of-gas if the size of the array becomes too large. function dynamicArray(uint input) public { uint[] memory bigArray = new uint[](1e6); for(uint i; i < bigArray.length; i++) { if(bigArray[i] == input) { x = 7; } } } In this example I created an array with 1e6 indices, then I tried to loop through the array to find a value that mat