为什么非常大的 If 语句会导致堆栈溢出

Why do very large If statements cause a stack overflow

我编写了一个跨越几千行的 if 语句,其中包含许多其他嵌套的 if 语句。当我没有注释掉很多错误时,我会收到堆栈溢出错误。我知道这是由于大小的原因,因为我发现这个特定的 if 语句只能处理 6 个不同的代码块 [表示“if (mainmenu1.equipment1A)”的代码块] [每个 if ((mainmenu1.player1equipment && ( Player1.job == 1)) ||... block] 如果有 7 个块,哪些块未被注释掉并不重要,无论哪个 7 块都会给我以下错误。虽然很奇怪,此代码是我正在开发的游戏中代码的一部分,并且代码在 switch 语句中处于完全不同的游戏状态情况下,启动时不应读取此代码。构建时我没有收到编译错误none 的代码被注释掉了,但是在启动时我得到了这个错误。它不应该是一个 运行 时间错误吗?但在那种情况下,我不应该得到错误,直到程序读取代码。我将包括一个代码示例,它总共大约 70,000 行,但它是重复的,所以想象一下下面的代码乘以 70。

SFML.exe 中 0x103B38B7 (ig7icd32.dll) 的第一次机会异常:0xC00000FD:堆栈溢出(参数:0x00000000、0x01072000)。 SFML.exe 中 0x103B38B7 (ig7icd32.dll) 的未处理异常:0xC00000FD:堆栈溢出(参数:0x00000000、0x01072000)。

SFML.exe 中 0x101AC0BE (ig7icd32.dll) 的第一次机会异常:0xC0000005:访问冲突写入位置 0x0107091C。 SFML.exe 中 0x101AC0BE (ig7icd32.dll) 的未处理异常:0xC0000005:访问冲突写入位置 0x0107091C。

//equipment text
    if (mainmenu1.equipmentmenu2)
    {
        if (executeequipmenttextonce)
        {
            executeequipmenttextonce = false;

            if (mainmenu1.rightselected)
            {
            if ((mainmenu1.player1equipment && (Player1.job == 1)) || (mainmenu1.player2equipment && (Player2.job == 1)) || (mainmenu1.player3equipment && (Player3.job == 1)))
            {
                if (mainmenu1.equipment1A)
                {
                    if (!mainmenu1.knightweapon1displayed && mainmenu1.knightweapon1)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon1displayed = true;
                        textDisplay16.text.setString("Broadsword");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw1));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon2displayed && mainmenu1.knightweapon2)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon2displayed = true;
                        textDisplay16.text.setString("Ea");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw2));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon3displayed && mainmenu1.knightweapon3)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon3displayed = true;
                        textDisplay16.text.setString("Zangetsu");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw3));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon4displayed && mainmenu1.knightweapon4)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon4displayed = true;
                        textDisplay16.text.setString("Silver Fleurette");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw4));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon5displayed && mainmenu1.knightweapon5)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon5displayed = true;
                        textDisplay16.text.setString("Lawliet");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw5));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon6displayed && mainmenu1.knightweapon6)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon6displayed = true;
                        textDisplay16.text.setString("Luminous Edge");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw6));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon7displayed && mainmenu1.knightweapon7)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon7displayed = true;
                        textDisplay16.text.setString("Heaven's Saber");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw7));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon8displayed && mainmenu1.knightweapon8)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon8displayed = true;
                        textDisplay16.text.setString("Crystal Edge");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw8));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon9displayed && mainmenu1.knightweapon9)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon9displayed = true;
                        textDisplay16.text.setString("Triple Zoro");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw9));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon10displayed && mainmenu1.knightweapon10)
                    {
                        mainmenu1.equipment1A = false;
                        mainmenu1.knightweapon10displayed = true;
                        textDisplay16.text.setString("Elsword");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw10));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY1);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                }
                else if (mainmenu1.equipment2A)
                {
                    if (!mainmenu1.knightweapon1displayed && mainmenu1.knightweapon1)
                    {
                        mainmenu1.equipment2A = false;
                        mainmenu1.knightweapon1displayed = true;
                        textDisplay16.text.setString("Broadsword");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY2);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw1));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY2);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon2displayed && mainmenu1.knightweapon2)
                    {
                        mainmenu1.equipment2A = false;
                        mainmenu1.knightweapon2displayed = true;
                        textDisplay16.text.setString("Ea");
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY2);
                        textDisplayArray16.push_back(textDisplay16);
                        textDisplay16.text.setString(to_string(mainmenu1.knightw2));
                        textDisplay16.text.setPosition(view1.getCenter().x - window.getSize().x / 2 + equipmentPositionX1 + equipmentstockpositionx, view1.getCenter().y - window.getSize().y / 2 + equipmentPositionY2);
                        textDisplayArray16.push_back(textDisplay16);
                    }
                    else if (!mainmenu1.knightweapon3displayed && mainmenu1.knightweapon3)
                    {
                        mainmenu1.equipment2A = false;
                        mainmenu1.knightweapon3displayed = true;
                        textDisplay16.text.setString("Zangetsu");

从技术上讲,if 语句不消耗堆栈 space。

if 表达式中的函数调用可能会导致 Whosebug。

在语句块中分配过多的局部变量可能会导致堆栈溢出。

在语句块中分配大变量可能会导致堆栈溢出。

没有限制的递归函数可能会导致堆栈溢出。

使用大量局部变量或参数的递归函数可能会导致堆栈溢出。

一个if语句一般被评估为比较指令和分支语句。这些指令不占用堆栈space。

您的错误可能位于其他地方。

使用调试器查找根本原因。