I asked this question in OZ forum and I'm asking it here again. The question is Why a state variable is wrapped in a struct in their Counters library. Please see the question in that post. Thanks.

1 Answer 1


Struct helps you to reduce the stack height while compiling. For compilations that have many threads, or say many local variables, (where stack support only 16) you may run into this error:-

Stack too deep when compiling inline assembly: Variable headStart is 1 slot(s) too deep inside the stack

But struct helps to reduce the number of local variables and consider them together as a single variable. And thus avoiding the error.

This article may help more about this.

  • Also, you may need keywords 'memory' or 'calldata' along with struct if you are using many variables into a struct. Eg;- struct memory myStruct {var1;var2;] Commented Jul 13, 2021 at 3:56
  • 1
    Hey thanks for the answer. Yes, this is a benefit of using a struct. Put it in a specific context, why is the Counters library using it then? There is only one state variable in the struct, so it is not helping reduce # of local variables. I thought it was used to anti-reading from storage directly, but it appeared not to be the case, after a few experiments.
    – Maxareo
    Commented Jul 14, 2021 at 6:54

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.