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

时间:2016-04-04 19:45:44

标签: c++ runtime-error stack-overflow sfml

我编写了一个if语句,其中包含几千行,其中包含许多其他嵌套的if语句。当我没有注释掉它时,我收到了堆栈溢出错误。我知道这是由于大小,因为我发现这个特殊的if语句只能处理6个不同的代码块[这个块说“if(mainmenu1.equipment1A)] [每个if中有10个((mainmenu1.player1equipment& &(Player1.job == 1))|| ... block]并且无论哪个块都没有被注释掉,如果有7个,无论哪个7都会给我这个跟随错误。这很奇怪虽然,这段代码是我正在处理的游戏中的代码的一部分,并且代码在switch语句中处于完全不同的游戏状态,并且在启动时不应该读取此代码。构建时我没有收到编译错误没有任何代码被注释掉,但在启动时我得到了这个错误。不是它不是运行时错误吗?但是在那种情况下,我不应该在程序读取代码之前得到错误。我将包括一个样本代码中,它总共大约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");

1 个答案:

答案 0 :(得分:7)

从技术上讲,if语句不会占用堆栈空间。

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

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

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

不受限制的递归函数可能导致堆栈溢出。

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

if语句通常被评估为比较指令和分支语句。这些指令不占用堆栈空间。

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

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