Using storage and memory efficiently
There are two types of memory area associated with contracts: storage and memory. Storage is a value store where all contract state variables are stored and are only changed by a transaction. Memory is a temporary storage location that is cleared for each message call. In this recipe, you will learn how to use these types efficiently, based on your requirements.
How to do it...
State variables are always stored in storage, and function arguments are always in memory.
- The memory location of a variable can be explicitly specified with the storage or memory keywords:
uint storage sum; uint memory calc;
Local variables created for
struct
,array
, or mapping types always reference storage by default. Modifying this local variable changes the actual storage data:
pragma solidity ^0.4.22; contract Storage { struct Name { string fName; string lName; } mapping(address => Name) public names; function setName(string _fName,string...