I know that generally it cost 5,000 gas to initialize memory variable. But simple smartcontract shows me that I misunderstand something.
pragma solidity ^0.4.24; contract MemConsumption { uint256 a; uint256 b; uint256 c; uint256 d; struct My { uint256 a; uint256 b; uint256 c; uint256 d; } function set(uint256 _a, uint256 _b, uint256 _c, uint256 _d) public { a = _a; b = _b; c = _c; d = _d; } function test() public view returns (uint256) { return calc(My(a,b,c,d)); } function calc(My my) private pure returns (uint256) { My memory m = My(my.a, my.b, my.c, my.d); return m.a + m.b + m.c + m.d; } } Function set expectedly cost ~100k gas - allocates 4 32-bytes words in storage.
But call of test confused me. I supposed it will cost me:
- transaction call cost (~20,000);
- gas for memory allocation of parameter
Myinstance (it is passed by value asmemoryone) i.e. 5,000 * 4 = 2,0000; - gas cost of memory instantiation of memory
mvariable;
But in Remix I see that transaction cost is 22745. Why?
calcfunction, aren't you (3 additions, to be accurate)? Those are not "for free" you know...