I have a contract that's doing a fairly complex calculation with some intermediate calculations, and I'm running into the "stack too deep" problem.
I've tried to use a memory array within the function to save temporary "local variables" instead of creating new local variables.
For example, instead of:
function usingLocalVars() {
// Get 3 uint values from a getter function call from another contract
// and save as local variables uint a, uint b, uint c:
var(a, b, c) = otherContract.getter(param);
// Perform some computation an a, b, and c and save as another variable d
uint d = someOperation(a, b, c);
/** other code with a return **/
}
Re-written using memory array:
function usingMemoryArray() {
// Get 3 uint values from a getter function call from another contract
// and store into memory array temp:
// create memory array
uint[4] memory temp;
// save values into memory array
(temp[0], temp[1], temp[2]) = otherContract.getter(param);
// Perform some computation an a, b, and c and save into temp[3]
temp[3] = someOperation(temp[0], temp[1], temp[2]);
/** other code with a return **/
}
So what I've tried to do:
- Eliminate almost all local variables by doing the above (memory array)
- Removed all named return variables, i.e.
returns (uint x, uint y, uint z)
=>returns (uint, uint, uint)
Current function summary:
- 1x function input paramater
- memory array of length 17 (all my "temp" variables)
- 1x
int
local variable - The function does call some other methods within the contract (such as
someOperation()
) above. Would this potentially be another cause of this problem? Needing to limit the local var used in other method calls (e.g.someOperation()
)?
Does using a memory array as I mentioned above actually help for this problem (stack too deep)?