It is a program generated by another system, therefore it could be repetitive and less efficient or intelligent that it should be. The finding is it have a large dependency and fell into a deep recursive function.
Because the vector could goes to 1000 elements and it must be contiguous. The Call stack while crash "Calculation::getItem(9)" > "Calculation::getItem(8)" > "Calculation::getItem(7)" > "Calculation::getItem(6)"... It crash before return the correct result of 100.
New question is : Any ways to resolve stack overflow without increasing stack size or changing the design of recursive function.
Calculation.cpp
std::optional<std::any> Calculation::getItem(int rowNo)
{
if(calculatedRow[rowNo].has_value())
{
return calculatedRow[rowNo];
}
switch(rowNo)
{
case 0 : return calculatedRow[rowNo] = 0;
case 1 : return calculatedRow[rowNo] = 100;
case 2 : return calculatedRow[rowNo] = getItem(1)
case 3 : return calculatedRow[rowNo] = getItem(2)
case 4 : return calculatedRow[rowNo] = getItem(3)
case 5 : return calculatedRow[rowNo] = getItem(4);
case 6 : return calculatedRow[rowNo] = getItem(5)
case 7 : return calculatedRow[rowNo] = getItem(6);
case 8 : return calculatedRow[rowNo] = getItem(7);
case 9 : return calculatedRow[rowNo] = getItem(8);
...
}
return 0;
}
Calculation.h
namespace Calculation
{
//private namespace
namespace
{
std::vector<std::optional<std::any>> calculatedRow(1000);
}
std::optional<std::any> getItem(int rowNo);
};