originally I got a stackoverflow exception in x86 mode. As I noticed that x64 would optimize tail recursion so i switched to compile in x64. And it worked out gracefully in debug mode.. However when I tried to run the release code.. it throwed stackoverflow again.. any possible reason?
Asked
Active
Viewed 463 times
8
-
4if you know your code can be tail optimized, why don't you do it yourself if you rely on it? – duedl0r Sep 02 '11 at 09:32
-
1@duedl0r: I more appreciate the functional programming coding style so I am kind of reluctant to change my tail recursions to some loops... – colinfang Sep 02 '11 at 09:36
-
maybe you could do it with some linq? seems to be functional style too :) – duedl0r Sep 02 '11 at 09:44
-
1@coilfang ehh because you can't. Int Foo(){ return Foo();} can run forever when tail call optimized doing the optimization by hand will take forever – Rune FS Sep 02 '11 at 09:49
-
Really, I don't like non-constructive comments like "why do you use that, use something I do". If colinfang uses functional style -- let him do it. If you don't know the solution, just don't make pointless discussion. @colinfang: could you make some simple tests? Write recursive factorial and check whether it can be TCO'd well. If it can, then Jon Skeet would be right („rules for when tail recursion optimizations are applied are complicated and ever-changing”), if not then I'd say it's a problem with project configuration (but really… dunno). – kgadek Sep 02 '11 at 10:10
-
If the language does not guarantee tail recursion, then you cannot rely on it. [Scheme](http://en.wikipedia.org/wiki/Scheme_%28programming_language%29) is an example for a language were tail call optimization is required from conforming language implementations. – Sebastian Mach Sep 02 '11 at 10:34
1 Answers
6
The rules for when tail recursion optimizations are applied are complicated and ever-changing.
I would strongly recommend that you don't rely on tail recursion from a correctness point of view.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
It was invented in 1977, gcc, g++, mvc++, scheme use it, so it could be said, that this is rather a problem with C♯ optimizations and not with TCO itself. Am I right? – kgadek Sep 02 '11 at 10:21
-
2@kgadek: If the language does not guarantee tail recursion, then you cannot rely on it. Scheme is an example for a language were tail call optimization is required from conforming language implementations. C# is not a language where this is required, so if you blow the stack because of too many recursions, there isn't anything you can complain about, except about the C# standard maybe. – Sebastian Mach Sep 02 '11 at 10:44