16

In solidity contracts, I can understand that based on variable usage and expensiveness, I can determine that which one should use (memory, storage or stack). But I faced some situations where only memory can be used and like that.

So my question is:

Where only memory can be used? Where only storage can be used? Where only stack can be used?

I couldn't find any documentation that states specific area should be used in any specific situation.

Thanks

22

Storage

Let's consider storage first. Storage is extremely expensive. It costs 20000 gas to set a storage location from zero to a value, and 5000 gas to change its value. (You do get some gas back for resetting a location to 0.) The reason is that a contract's storage values are stored on the blockchain forever which has a real-world cost. It's also 200 gas to read a word, compared with 3 for memory.

So for storage, use it only when you really must.

When must you use storage? For values that have to be persisted between different contract calls. For example the balances of accounts for an ERC20 token. Storage updates are state updates of your contract and are not really for temporary data. Storage of one contract can also be read by another contract, or by querying the blockchain without running the contract.

You can think of it like hard disk/SSD, if you like. Storage retains its value when the computer's not running.

Memory

Memory is relatively much cheaper than storage. It costs 3 gas to read or write a word, plus some gas if you are expanding memory. For a few KB it's very cheap indeed, but the cost goes up quadratically the more you use: a megabyte of memory will cost a couple of million gas.

However, the memory is only accessible during contract execution; once execution is finished, its contents are discarded.

When should you use memory? Pretty much for everything non-permanent. It's the general workhorse.

You can think of it like a computer's RAM. It vanishes when the computer is not running.

Stack

The costs of storing data on the stack are similar to those for memory. Sometimes there will be less overhead from having to push memory addresses to the stack; sometimes there will be more overhead as the compiler has to shuffle things around on the stack. The stack has a maximum capacity of 1024 items, but only the top 16 are easily accessible. If you run out of stack then contract execution will fail. The compiler generally uses it for intermediate values in computations and other scratch quantities.

On the whole, I'd leave the stack to the compiler to work out.

The stack also does not persist after contract termination.


All the info above is buried in the Yellow Paper - I've read it so you don't have to :-)

  • Thanks for the valuable information. Can you please clerify, if we define a variable as "uint256 x", by deailt it would be storage, memory or stack? – Abhishek Aug 5 '17 at 9:39
  • It's not straightforward - it depends where and how you declare it. More info is here: solidity.readthedocs.io/en/develop/… - read through the whole section. – benjaminion Aug 5 '17 at 10:05
  • Could you also please explain dynamic arrays bytes[] and where that is stored. – William Entriken Jan 31 '18 at 2:52
  • Can you explain for me when should we use stack and memory. thanks – hqt May 11 '18 at 18:33
  • 1
    Given that users are limited to 16 total arguments or variables per function - function arguments, return values, and local variables - that tells me that the docs don't reveal the whole story. Even if function arguments are copied into memory, they must be pushed into the stack. The opposite seems to hold true for complex local variable declarations: they can be declared as copied in memory, but I suspect a pointer to the data is placed in stack as well. To the OP, you can't decide for the most part where the data will be stored, unless you can confidently use the assembly code commands. – GViz Jan 9 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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