在C#中使用递归 [英] Using Recursion in C#

查看:129
本文介绍了在C#中使用递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免stackoverflows使用递归时是否有任何一般规则?

Are there any general rules when using recursion on how to avoid stackoverflows?

推荐答案

,你将能够多少次递归将取决于:

How many times you will be able to recurse will depend on:


  • 的堆栈大小(通常是1MB IIRC,但二进制可以手动编辑,我不会推荐这么做)

  • 多少堆栈递归用途的每个级别(10未捕获的Guid 局部变量的方法将采取更多堆栈比不具有任何局部变量的方法,例如)

  • 您正在使用的JIT - 有时JIT的将会的使用尾递归,其他有时它不会。规则很复杂,我不记得他们。 (有一个的博客文章由大卫Broman从2007年回来,一个MSDN页面同一作者/日期,但他们可能会过时的现在。)

  • The stack size (which is usually 1MB IIRC, but the binary can be hand-edited; I wouldn't recommend doing so)
  • How much stack each level of the recursion uses (a method with 10 uncaptured Guid local variables will be take more stack than a method which doesn't have any local variables, for example)
  • The JIT you're using - sometimes the JIT will use tail recursion, other times it won't. The rules are complicated and I can't remember them. (There's a blog post by David Broman back from 2007, and an MSDN page from the same author/date, but they may be out of date by now.)

如何避免堆栈溢出?不要递归太远:)如果你不能合理地确定你的递归会而不去很远的结束(我在等10多个担心,虽然这是非常安全的),然后把它改写,以避免递归。

How to avoid stack overflows? Don't recurse too far :) If you can't be reasonably sure that your recursion will terminate without going very far (I'd be worried at "more than 10" although that's very safe) then rewrite it to avoid recursion.

这篇关于在C#中使用递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆