Search code examples
stacksoliditysmartcontractsremix

CompilerError: Stack too deep, try removing local variables


I am trying to do a dapp project.

I have an error about stack too deep, but I don't know how can I solve this problem.

CompilerError: Stack too deep, try removing local variables. --> contracts/proje.sol:145:5: | 145 | function addRecord(address _addr, s ... gedOn, string memory ipfs) public { | ^ (Relevant source part starts here and spans across multiple lines).

And actually, struct will consist of more data than there is. If this gives an error in the number of data, what do we do when we want to enter too much data?

contract Patient is Clinic {
    uint256 public p_index = 0;

    struct Records {
        string cname;

        string l_cadence;
        string r_cadence;
        string n_cadence;

        string l_dsupport;
        string r_dsupport;
        string n_dsupport;

        string l_footoff;
        string r_footoff;
        string n_footoff;

        string l_steptime;
        string r_steptime;
        string n_steptime;


        string admittedOn;
        string dischargedOn;
        string ipfs;
    }

    struct patient {
        uint256 id;
        string name;
        string phone;
        string gender;
        string dob;
        string bloodgroup;
        string allergies;
        Records[] records;
        address addr;
    }

    address[] private patientList;
    mapping(address => mapping(address=>bool)) isAuth;
    mapping(address=>patient) patients;
    mapping(address=>bool) isPatient;

    function addRecord(address _addr, string memory cname, string memory l_cadence, string memory r_cadence, string memory n_cadence, string memory l_dsupport, string memory r_dsupport, string memory n_dsupport, string memory l_footoff, string memory r_footoff, string memory n_footoff, string memory l_steptime, string memory r_steptime, string memory n_steptime, string memory admittedOn, string memory dischargedOn, string memory ipfs) public {
        
    }
}

Solution

  • Compiler error is for parameters number about addRecord() function. To solve this problem, you can use directly Records struct in this way:

    function addRecord(address _addr, Records memory record) public {
        // your logic    
    }
    

    Another options to solve this problem is to change type function from public to internal, like:

    function addRecord(address _addr, string memory cname, string memory l_cadence, string memory r_cadence, string memory n_cadence, string memory l_dsupport,
         string memory r_dsupport, string memory n_dsupport, string memory l_footoff, string memory r_footoff, string memory n_footoff, string memory l_steptime, 
         string memory r_steptime, string memory n_steptime, string memory admittedOn, string memory dischargedOn, string memory ipfs) internal {
            // your logic
        }
    

    NOTE: Before to do this modify, see the difference between public and internal types function here.