LoginSignup
9
3

More than 5 years have passed since last update.

Solidityの変数の格納領域(storage, memory, stack)

Posted at

アカウント毎に3つの領域(storage, memory, stack)を持つようだ。

storage

ブロックチェーンに記録され、永続化される領域。
KVS(256bit => 256bit)
ブロックチェーンに記録されるため、高コスト(高gas)である。
struct, array, mappingの型のローカル変数はデフォルトでstorageに格納される。

memory

各メッセージ(contractのインスタンス?)に対して、毎回新たに作られる領域。なので、永続化されない。
storageより低コストだが、メモリ領域を拡張する際にコストがかかるようだ。
関数の引数はデフォルトでmemoryに格納される。

stack

小さな値を内部で保持する際に使う。
コストはほとんど掛からないが、サイズに制約がある。
struct, array, mapping以外のvalue型ローカル変数(参照型で無いものという意味?)はデフォルトでstackに格納される。

まとめ

変数が格納される領域によって、ライフタイムとコスト(gas)が異なるようなので、考慮した設計にすることが必要。

課題

・stackのサイズについてよくわかっていない

参考:
solidity official:
https://solidity.readthedocs.io/en/latest/frequently-asked-questions.html#what-is-the-memory-keyword-what-does-it-do
http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html?highlight=storage%20memory#storage-memory-and-the-stack
(英語がスラスラ読めるようになりたいなぁ。)

9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3