Local Variables (Storage v Memory)
Last updated
Last updated
Only complex data types (arrays and structs) default to storage inside functions, while all others default to memory.
A consequence of this design difference is that storage is dynamic and memory is not.
Because arrays and structs are complex and could be of variable length, they are defaulted to storage, which has this key:value behaviour.
Simpler variables like bool, uint, etc are not variable in length, and are therefore defaulted to memory, which is . So, think of the design choice as a compromise between flexibility and cost.
It is possible to , but once created they cannot be resized (check out the Allocating Memory Arrays section).
Compiling with the optimizer leads to the same bytecode and gas cost.
If the optimizer is not used, the compiler would produce more bytecode, and the gas cost would be higher.