0

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

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.

2
  • 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;] Jul 13 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
    Jul 14 at 6:54

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.