Why is storing in an array the first time in solidity costs higher than the other times? The other times it has a constant cost which is less than the first time.
(I'm sure this has come up before. If someone can find a previous answer then please duplicate... )
From the Yellow Paper, Appendix G, you'll see the following line:
Gsset - 20000 - Paid for an SSTORE operation when the storage value is set to non-zero from zero.
Meaning the first time you write a variable to storage, it's more expensive by 20,000 gas. Subsequent updates to the variable are cheaper.
As JBrouwer has pointed out in the comments, for
structs arrays you also need to store the length, so you're writing to two initially-zeroed storage slots rather than one, equating to 40,000 extra gas.