简体   繁体   English

我正在制作一个控制台应用程序游戏 (C#),但我遇到了堆栈溢出错误。 如何避免?

[英]I'm making a Console Application Game (C#), and I'm running into Stack Overflow Error. How to avoid?

My game has multiple methods for each state of the game and these methods call each other where applicable.我的游戏对游戏的每个状态都有多种方法,这些方法在适用的情况下相互调用。 This is causing a Stack Overflow Error after a while.这会在一段时间后导致堆栈溢出错误。 Is there any way to avoid this, perhaps a workaround?有什么办法可以避免这种情况,也许是一种解决方法?

Edit: As the user below suggested my code is basically:编辑:由于下面的用户建议我的代码基本上是:

void a()
{
    b();
}

void b()
{
    a();
}

In this code I intended to use recursion in order to change game states.在这段代码中,我打算使用递归来改变游戏状态。 Is there any way to reset the stack so it doesn't cause an overflow error and then return to the methods.有没有办法重置堆栈,这样它就不会导致溢出错误,然后返回到方法。

Possible Fix: (Did this just delay the Stack Overflow or has it gotten rid of it?)可能的修复:(这只是延迟了堆栈溢出还是已经摆脱了它?)

int tracker;
void start()
{
    a();
    resetter();
}

void a()
{
    b();
}

void b()
{
    if (tracker > 10)
    {
        tracker = 0;
        return;
    }
    else
        tracker++;
    a();
}

void resetter()
{
    start();
}

I assume you're doing something like:我假设你正在做这样的事情:

void A()
{
  B();
}
void B()
{
  A();
}

This, by definition, will always cause a stack overflow.根据定义,这将始终导致堆栈溢出。 This is basicly because of how the stack works.这主要是因为堆栈的工作方式。 Say you call A first, the moment this method calls B , the parameters/variables/... for this method are put on the stack.假设您先调用A ,在此方法调用B的那一刻,此方法的参数/变量/... 被放入堆栈中。 Then, this method ( B ), calls ´A´;然后,此方法 ( B ) 调用“A”; the moment that happens, it will put its parameters on the stack.发生这种情况的那一刻,它将把它的参数放在堆栈上。

The data on the stack is cleared after a method returns.方法返回后,堆栈上的数据被清除。 But these two methods will never ever return... They keep on calling one another, something like:但这两种方法永远不会返回......它们不断地相互调用,例如:

A()
  --> B()
    --> A()
      --> B()
        --> A()
          --> B()
            --> A()
              --> B()
               --> A()

As you can see, more and more stuff is put on the stack, causing your overflow.如您所见,越来越多的东西被放入堆栈,导致您的溢出。

You will want your recursion to stop to fix this.你会希望你的递归停止来解决这个问题。 You can do this by introducing a stopping condition in either method which will return .您可以通过在任一方法中引入停止条件来实现此目的,该条件将return

If you wish a more concrete answer to your question, please add some code.如果您希望更具体地回答您的问题,请添加一些代码。 But since you're talking about A calling B, which is calling A, the above pseudocode should be adequate.但是既然你说的是A调用B,也就是调用A,那么上面的伪代码就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在制作ASP .NET MVC应用程序,在运行项目时出现此错误 - I'm making ASP .NET MVC application, I'm getting this error when running the project 如何避免更改堆栈大小并避免C#中出现堆栈溢出 - How do I avoid changing the Stack Size AND avoid getting a Stack Overflow in C# 我在 Unity 中制作游戏时遇到了 C# 中的 OnCollisionEnter 函数问题 - I'm having a problem with the OnCollisionEnter function in C# while making a game in Unity C#if语句在主机游戏中不起作用(我是初学者,所以可能是完全错误的) - C# if statements not working in console game (I'm a beginner so it may be completely wrong) 我正在统一制作 FPS 游戏并不断收到 1 个错误 - I'm making an FPS game in unity and keep getting 1 error C#制作足球游戏控制台应用程序 - C# Making a Football Game Console Application 如何检测我是否在控制台中运行 - How do I detect if I'm running in the console 在 C# 中制作我的第一个计算器,我被卡住了 - Making my first calculator in C# and I'm stuck 当我在 C# 中创建 JSON 帖子时,如何返回 HTTP 状态代码? - How do I return the HTTP status code when I'm making a JSON post in C#? 我是初学者,编写控制台程序 我正在尝试构建计算器并需要帮助 (C#) - I'm a beginner and write console programs I'm trying to build a calculator and need help (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM