4

Are local variables stored in memory or on the stack in Solidity?

If memory, is the memory freed after the function returns?

3

For value types (booleans, integers, addresses ...) it's memory. For complex types (arrays, structs, maps) the default location depends on the context and can be overridden by memory and storage keywords.

Whenever there is a need to perform some computation on the variable (adding, multiplying, negating ...) the variable will be moved from the memory/storage to the stack (along with other variables needed for the operation), the result will be put back to the memory/storage.

After returning from a function the free memory pointer will be updated so the unused memory can be reused. The return value of a function can also be located in the memory or storage.

1

The EVM has a stack-based architecture. It has a volatile memory where items are stored as byte arrays. The size of each stack item is 256-bit and the stack has a maximum size of 1024.

The memory is freed but your program will stop running if the new stack size is greater than 1024.

White paper - https://github.com/ethereum/wiki/wiki/White-Paper

Yellow paper - https://ethereum.github.io/yellowpaper/paper.pdf

  • Are you sure memory is freed? I've seen that memory only expands (never shrinks). If you mean by "freed" that the memory can be reused for other variables, then I agree. – Shuzheng Dec 1 '17 at 14:24
0

All simple local data variables(booleans, integers, addresses ...) will store in stack. Some complex data variables(Like arrays, string, struct) by default will save in storage. Thats a reason Remix or solidity compiler will throw warning to us.

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.