Function Parameters Causing Stack Overflow?

-3

When I first coded this function, I used a whole lot of global vectors and objects and it worked just fine. Recently I improved my code so that it passes objects and vectors by reference and value but now I get a stack overflow as soon as my program calls this function. I know that the problem lies in the parameters. I updated a few of my other functions to use parameters rather than global objects and global vectors as well and they work just fine. If I had to take a guess I would say the error is caused by too many occurrences of the parameters within the function, but what do I know.
It is a very large function but i will include a sample of it:

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);

    }
}
}

the actual function is much longer than this but it's consistent with whats shown here.

Here's how the function is called:

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

and here is the error I get:

First-chance exception at 0x04465BE7 in SFML.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00922000).
c++
function
parameters
stack-overflow
asked on Stack Overflow Jun 25, 2016 by yeshuasaves • edited Jun 25, 2016 by yeshuasaves

1 Answer

3

Probably because you pass some of the parameters (mainmenu1, textDisplay16, textDisplay17) by value rather than by reference.

These parameters may contain huge data in them (i.e. a string of millions characters, or a list of million items). This also can be due to a bug.

On a side note, your coding style is horrible. Your function repeats the same lines many times. Instead your should make the repeating set of actions another function.

answered on Stack Overflow Jun 25, 2016 by user31264 • edited Jun 25, 2016 by user31264

User contributions licensed under CC BY-SA 3.0