-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Expand file tree
/
Copy patharray_memory_index_access.sol
More file actions
36 lines (31 loc) · 1016 Bytes
/
array_memory_index_access.sol
File metadata and controls
36 lines (31 loc) · 1016 Bytes
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
contract C {
function index(uint256 len) public returns (bool)
{
uint[] memory array = new uint[](len);
for (uint256 i = 0; i < len; i++)
array[i] = i + 1;
for (uint256 i = 0; i < len; i++)
require(array[i] == i + 1, "Unexpected value in array!");
return array.length == len;
}
function accessIndex(uint256 len, int256 idx) public returns (uint256)
{
uint[] memory array = new uint[](len);
for (uint256 i = 0; i < len; i++)
array[i] = i + 1;
return array[uint256(idx)];
}
}
// ----
// index(uint256): 0 -> true
// index(uint256): 10 -> true
// index(uint256): 20 -> true
// index(uint256): 0xFF -> true
// gas irOptimized: 108272
// gas legacy: 181536
// gas legacyOptimized: 117430
// accessIndex(uint256,int256): 10, 1 -> 2
// accessIndex(uint256,int256): 10, 0 -> 1
// accessIndex(uint256,int256): 10, 11 -> FAILURE, hex"4e487b71", 0x32
// accessIndex(uint256,int256): 10, 10 -> FAILURE, hex"4e487b71", 0x32
// accessIndex(uint256,int256): 10, -1 -> FAILURE, hex"4e487b71", 0x32