c# - Recursive method cause stackoverflow -
i have simple methode this:
public int method(int a) { if(// something){ methode(a); } }// end
as see recursive method. problem , when depth of calling increases, visual studio throws stackoverflow exeption.
how can solve problem? there way save return address , local data manually , implement customized stack??
please me.
i should note dont want change method non recursive type.
best regards
yes, in c# (and in java etc) there stack<t> class. method can create stack , store argument there. iterate on stack until it's empty.
while introduces iterative loop, algorithm still recursive. (that is, depth first instead of breadth first)
of course need make sure algorithm terminates eventually. way increase stack space above operating system gives you. windows allocates amount of stack space each process suffices applications. if need more, can implement own stack-like data structure on heap. heap limited available ram , "bitness" of application.
Comments
Post a Comment