c# - Recursive function causing an overflow despite it's not infinite -
the following function wrote causing program crash due stack overflow, although recursion finite.
public static void key(char[] chars, int i, int l, string str) { string newstr=null; for(int j=0; j<l; j++) newstr+=chars[i/(int)math.pow(68, j)%68]; if(newstr==str) return; key(chars, ++i, l, newstr); }
when call method these parameters, goes fine:
key(chars, 0, 4, "aaaa");
but when comes bigger number of calls, throws stackoverflowexception
. assume problem althogh method finite call stack gets filled before the methods' job done. have few questions that:
why functions don't clear stack, no longer needed, don't return value.
and if so, there way clear stack manually? tried
stacktrace
class it's helpless in case.
1) function clears when has ended execution. calling key inside of means every call on stack until last call ends, @ stage end in reverse order.
2) can't clear stack , carry on call.
Comments
Post a Comment