If it's appropriate to the situation, and if the compiler isn't doing
this for you already, you could always go in and hack the IL such that
the recursive solution implements a tail call methodology. C# has no way
to do so (and honestly this should be done by the compiler) but a tail
call in a strategic place would accomplish the same thing and minimize
redundant stack frames.

But this can't be done in all recursive scenarios, obviously, as a tail
call is only appropriate if the current method is returning the
unmodified return value of the method it's calling.

Has anyone checked to find out whether this situation is optimized by
VS.Net? I would imagine it was, but I've never bothered to check.

-----Original Message-----
From: The DOTNET list will be retired 7/1/02
[mailto:[EMAIL PROTECTED]] On Behalf Of franklin gray
Sent: Wednesday, June 05, 2002 11:47 AM
To: [EMAIL PROTECTED]
Subject: Re: recursive stack trace

thanks all.

-----Original Message-----
From: Murphy, James [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, June 05, 2002 11:17 AM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] recursive stack trace


> -----Original Message-----
> From: franklin gray [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, June 05, 2002 12:14 PM


> I have a recursive function that calls itself many times.  If
> an error occurs, I notice that the call stack is very large.
>
> Two questions:
>
> 1) Will this stack overflow?

Yes but the recursive function has to be called many many times - like
an
infinite loop kind of many.  I don't have any feel for the actual number
of
calls this would take since it depends on the method you are calling and
the
amount of stack space it requires.  If you get close to this limit its
most
likely a bug.


> 2) Is there a way to clear the stack after each call?

I doubt you wan to do that!  The stack info contains the critical data
required to unwind the stack after recursion stops.

Instead of using recursion you can do the same thing with a loop and a
stack
data structure.  At each iteration instead of recursing push the
iteration
data onto a the top of a stack data structure (or the end of an
ArrayList
lets say).


Jim Murphy
http://www.ironringsoftware.com

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to