Data Storage in Solidity
storage
and memory
are two ways of storing data in Solidity.
memory
is like RAM storage, its data is volatile, it gets destroyed at end of every call and created at start of every function call, similar to RAM is erased when the computer shuts down and data added when the computer is starting and running.
storage
is like Hard Disk, its data is not volatile, the data once set, will persists in future function calls, similar to Hard Disk, whose data is not erased/destroyed when computer shuts down.
Generally most of the data types are storage
except that of arguments of a function and explicitly defined memory
variables.
- State variables and Local Variables of structs, array are always stored in
storage
by default.
uint totalCount;address owner;address[] participants;string messageData;struct person {
string name;
string age;
}
person[] personDatabase;mapping(uint => person) register;
totalCount
, owner
, participants
, messageData
, personDatabase
, register
are all of type storage
.
- Function arguments are in
memory
.
string memory messageData;function Action1(uint minimumBet, uint maxAmountOfBets, string memory userDescription) {...}
minimumBet
, maxAmountOfBets
, userDescription
are all of type memory
- Whenever a new instance of an array is created using the keyword
memory
, a new copy of that variable is created on every call, hence, changing the array value of the new instance does not reflect as the array is reset back to the origin at every call.
New to trading? Try crypto trading bots or copy trading