0

I was learning about arrays in Solidity . I am not getting exactly when should I create an array in memory and when in storage . So far I only know that an array created in memory is fixed in size and I cannot use push and pop methods in that case .

Can anyone explain the difference between an array created in memory ( what it is , how it behaves , gas costs etc . ) and an array created in storage ?

Reference : https://www.youtube.com/watch?v=vTxxCbwMPwo

1 Answer 1

1

The main differences are:

  1. Storage, persistent contract state, costly
  2. Memory, transient, fixed length only, cheaper

Generally, use storage to record things and memory as a scratch pad.

Hope it helps.

2
  • Hi @Rob , " Generally, use storage to record things and memory as a scratch pad. " Can you explain this line a bit more ? May 7, 2023 at 19:40
  • Well, memory is cheaper but it is erased after txn finishes. Storage is more like a database. It will last but i/o is more expensive. May 8, 2023 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.