函数参数导致堆栈溢出?

时间:2016-06-25 02:35:33

标签: c++ function parameters stack-overflow

当我第一次编写这个函数时,我使用了大量的全局向量和对象,它运行得很好。最近我改进了我的代码,以便通过引用和值传递对象和向量,但是现在我的程序调用此函数时会出现堆栈溢出。我知道问题在于参数。我更新了我的一些其他函数来使用参数而不是全局对象和全局向量,它们工作得很好。如果我不得不猜测,我会说错误是由函数中出现的参数太多引起的,但我知道的是什么。
这是一个非常大的功能,但我将包括一个样本:

void armorDisplayText(mainmenu mainmenu1, armorshop &armorshop1, std::vector<textDisplay> &textDisplayArray16, std::vector<textDisplay> &textDisplayArray17, textDisplay textDisplay16, textDisplay textDisplay17)
{
if (armorshop1.firstitem)
{
    if (!armorshop1.armor1displayed && armorshop1.armor1)
    {
        armorshop1.firstitem = false;
        armorshop1.armor1displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand1string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor1cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor2displayed && armorshop1.armor2)
    {
        armorshop1.firstitem = false;
        armorshop1.armor2displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand2string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor2cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor3displayed && armorshop1.armor3)
    {
        armorshop1.firstitem = false;
        armorshop1.armor3displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand3string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor3cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor4displayed && armorshop1.armor4)
    {
        armorshop1.firstitem = false;
        armorshop1.armor4displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand4string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor4cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor5displayed && armorshop1.armor5)
    {
        armorshop1.firstitem = false;
        armorshop1.armor5displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand5string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor5cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor6displayed && armorshop1.armor6)
    {
        armorshop1.firstitem = false;
        armorshop1.armor6displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand6string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor6cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor7displayed && armorshop1.armor7)
    {
        armorshop1.firstitem = false;
        armorshop1.armor7displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand7string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor7cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor8displayed && armorshop1.armor8)
    {
        armorshop1.firstitem = false;
        armorshop1.armor8displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand8string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor8cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor9displayed && armorshop1.armor9)
    {
        armorshop1.firstitem = false;
        armorshop1.armor9displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand9string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor9cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor10displayed && armorshop1.armor10)
    {
        armorshop1.firstitem = false;
        armorshop1.armor10displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand10string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor10cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor11displayed && armorshop1.armor11)
    {
        armorshop1.firstitem = false;
        armorshop1.armor11displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand11string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor11cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor12displayed && armorshop1.armor12)
    {
        armorshop1.firstitem = false;
        armorshop1.armor12displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand12string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor12cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor14displayed && armorshop1.armor13)
    {
        armorshop1.firstitem = false;
        armorshop1.armor14displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand13string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor13cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor13displayed && armorshop1.armor14)
    {
        armorshop1.firstitem = false;
        armorshop1.armor13displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand14string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor14cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor15displayed && armorshop1.armor15)
    {
        armorshop1.firstitem = false;
        armorshop1.armor15displayed = true;
        textDisplay16.text.setString(mainmenu1.offhand15string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor15cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor16displayed && armorshop1.armor16)
    {
        armorshop1.firstitem = false;
        armorshop1.armor16displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead1string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor16cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor17displayed && armorshop1.armor17)
    {
        armorshop1.firstitem = false;
        armorshop1.armor17displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead2string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor17cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor18displayed && armorshop1.armor18)
    {
        armorshop1.firstitem = false;
        armorshop1.armor18displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead3string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor18cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor19displayed && armorshop1.armor19)
    {
        armorshop1.firstitem = false;
        armorshop1.armor19displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead4string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor19cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor20displayed && armorshop1.armor20)
    {
        armorshop1.firstitem = false;
        armorshop1.armor20displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead5string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor20cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor21displayed && armorshop1.armor21)
    {
        armorshop1.firstitem = false;
        armorshop1.armor21displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead6string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor21cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor22displayed && armorshop1.armor22)
    {
        armorshop1.firstitem = false;
        armorshop1.armor22displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead7string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor22cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor23displayed && armorshop1.armor23)
    {
        armorshop1.firstitem = false;
        armorshop1.armor23displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead8string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor23cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor24displayed && armorshop1.armor24)
    {
        armorshop1.firstitem = false;
        armorshop1.armor24displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead9string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor24cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor25displayed && armorshop1.armor25)
    {
        armorshop1.firstitem = false;
        armorshop1.armor25displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead10string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor25cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor26displayed && armorshop1.armor26)
    {
        armorshop1.firstitem = false;
        armorshop1.armor26displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead11string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor26cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor27displayed && armorshop1.armor27)
    {
        armorshop1.firstitem = false;
        armorshop1.armor27displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead12string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor27cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
    else if (!armorshop1.armor28displayed && armorshop1.armor28)
    {
        armorshop1.firstitem = false;
        armorshop1.armor28displayed = true;
        textDisplay16.text.setString(mainmenu1.knighthead13string);
        textDisplay16.text.setPosition(100, 110);
        textDisplayArray16.push_back(textDisplay16);
        textDisplay16.text.setString(std::to_string(armorshop1.armor28cost));
        bounds = textDisplay16.text.getLocalBounds();
        textDisplay16.text.setPosition(380 - bounds.width, 110);
        textDisplayArray16.push_back(textDisplay16);

    }
}
}

实际功能比这长得多,但它与这里显示的内容一致。

这是函数的调用方式:

armorDisplayText(mainmenu1, armorshop1, textDisplayArray16, textDisplayArray17, textDisplay16, textDisplay17);

这是我得到的错误:

First-chance exception at 0x04465BE7 in SFML.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00922000).

1 个答案:

答案 0 :(得分:3)

可能是因为您通过值而不是引用传递了一些参数(mainmenu1,textDisplay16,textDisplay17)。

这些参数可能包含大量数据(即数百万字符的字符串,或百万项的列表)。这也可能是由于一个错误。

另一方面,你的编码风格很糟糕。您的函数重复多次相同的行。相反,你应该重复一组动作。