1

Got a "stack too deep" error, and saw on stack exchange that I could try and use memory variables instead of local variables, and moved 8 uint into a uint[8], no error anymore. Why does that work while using individual unit did not?

Code example

uint a;
uint b;
uint c;
uint d;
uint e;
uint f;
uint g;
uint h;

"using too many variables, stack too deep"

and with

uint[8] memory abcdefgh;

no problem

1
  • Can you add more detailed code/ context? The declarations on themselves seem to work fine for me.
    – DaveIdito
    Commented Nov 6, 2018 at 1:07

1 Answer 1

3

In the first case, your individual primitives are local variables, placed on the program stack. The maximum stack depth is limited: those variables may make your program reach that limit, generating a stack overflow error.

In the second case, by default, Solidity will generate code to store the array in storage, which has virtually an infinite capacity. Therefore, there won't be any error.

You can make that explicit with the storage attribute. You can also tell the compiler to place the array in memory with the memory specifier: uint[8] memory abcdefgh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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