PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 25, 2022

[FIXED] why exactly should we use memory or calldata for arrays ,strings, and structs in a function

 September 25, 2022     blockchain, ethereum, smartcontracts, solidity     No comments   

Issue

is there any reason why we should not store strings as storage in a function? I do not get the purpose of using it.

If anyone can explain it's greatly appreciated.


Solution

  • Variables declared as function parameters are either stored at a call data or memory data location.

  • Variables declared inside functions, are stored in a memory data location.

Every operation in a smart contract costs you money. So you have to be careful when you design a smart contract. Your variables that set in a storage will cost you no matter what, but variables inside functions will cost you when your functions are being called. It is kinda paid as you go.

  • Variables that are declared as a state are always stored in the storage.

When creating state variables in any application, you have to be very careful not to modify them. Because if you modify them in one part of your code, another part of your code may not give you the desired result, and you will create a lot of bugs.

Imagine, you identified your function as public, and variables and arguments are stored in storage. So anyone who calls your function will modify your state variables. To keep the state variables safe, inside functions we copy them into memory and use this copied value.



Answered By - Yilmaz
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing