0

I have a struct with two variables bool, nine bytes 32 and three uint. The problems I want to add another bytes32 but I get this error "InternalCompilerError: Stack too deep, try removing local variables."

struct xxxx{

    bytes32 id;
    bool x;
    //bool w;
    uint date;

    bytes32 a;
    bytes32 b;
    bytes32 c;
    bytes32 d;
    bytes32 e;


    bytes32 f;//Nuevo
    bytes32 g;//NUEVO
    bytes32 h;//NUEVO

    uint j;
    uint k;

    bool active;
    //bool active;
}
  • Could you share your code? Or a reduced version of your code that still gives this error? – Jesse Busman Dec 22 '18 at 21:53
  • Usually, the "Stack too deep" error happens when you have too many local variables. – Jesse Busman Dec 22 '18 at 21:54
  • I know what the error is, I also know that if you organize the bytes of the variables you can add other variables, I have tried but still I get an error: C. – Nicolas Flores Muñoz Dec 22 '18 at 22:34
  • @NicolasFloresMuñoz The error is not in the structure definition but the way you are using it. I've created structures with more than 30 members without problems. – Ismael Dec 23 '18 at 3:11
0

The problem isn't in the struct. It's probably in the default getter function you get when cast a variable as the type defined in the struct and set visibility to public. In this case, the compiler tries to make a function that will return the scaler variables in the struct, and here you run into stack limitation.

Here is a (useless) contract with the struct extended to include more variables. There is no problem.

pragma solidity 0.5.1;

contract Stack {

    struct xxxx {

    bytes32 id;
    bool x;
    bool w;
    uint date;

    bytes32 a;
    bytes32 b;
    bytes32 c;
    bytes32 d;
    bytes32 e;


    bytes32 f;//Nuevo
    bytes32 g;//NUEVO
    bytes32 h;//NUEVO

    uint j;
    uint k;

    bool active;
    bool b1;
    bool b2;

    bytes32 added;
  }

  // xxxx public s;
}

If you uncomment the last line, then we have a problem.

You could, therefore, refrain from using public and just create your own getters to return portions of instances.

I would remiss if I didn't mention that storing tonnes of metadata in contracts is usually not recommended.

Hope it helps.

  • I think that if you try to instantiate a local variable of this type (i.e., struct xxxx inside a function), then you're likely to get the same "Stack Too Deep" problem. So it's not only about a getter function, and can appear in a bunch of other scenarios. – goodvibration Dec 23 '18 at 8:23
  • The OP didn't say what the scenario is so I took a guess about what might be causing it. – Rob Hitchens B9lab Dec 23 '18 at 13:16
  • Also, passing such struct as input argument to another function will yield the same effect (though this is not supported in Solidity 0.4.24, but I guess possibly supported in later versions). – goodvibration Dec 23 '18 at 13:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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