-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.sol
50 lines (37 loc) · 1.26 KB
/
Storage.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Data {
uint[] public arr;
mapping(uint => address) map;
struct MyStruct {
uint foo;
}
mapping(uint => MyStruct) myStructs;
function _f(
uint[] storage _arr,
mapping(uint => address) storage _map,
MyStruct storage _myStruct
) internal {
// We can do anything with state variable here
}
function f() public {
// calling _f with state variables
_f(arr, map, myStructs[1]);
// getting a struct from a mapping
MyStruct storage myStruct = myStructs[1];
// creating a struct in a memory
MyStruct memory myMemStruct = MyStruct(0);
}
function g(uint[] memory _arr) public returns (uint[] memory) {
// We can do anything using state variables
}
function h(uint[] calldata _arr) external {
// We can d0 anything using state variables
}
}
/*
storage => Stored in a Blockchain
Memory => The variable are stored and have a lifetime
Stack => All variables declared in a function are stored in a fixed size stack
CallData => Similar to memory , this variables have a lifetime but are immutable
*/