Are local variables stored in memory or on the stack in Solidity?
If memory, is the memory freed after the function returns?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
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
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.
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
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.