5

Here is some code:

void main()
{
    GameEngine ge("phil", "anotherguy");
    string response;
    do {
        ge.playGame();
        cout << endl << "Do you want to (r)eplay the same battle, (s)tart a new battle, or (q)uit? ";
        cin >> response;
    } while(response == "r" || response == "R" || response == "s" || response == "S" );
}

GameEngine::GameEngine(string name1, string name2)
{
    p1Name = name1;
    p2Name = name2;
}

void GameEngine::playGame()
{
    cout << "PLAY GAME" << endl;
    Army p1, p2;
    Battlefield testField;
    RuleSet rs;

    int xSize = 13; // Number of rows
    int ySize = 13; // Number of columns

    loadData(p1, p2, testField, rs, xSize, ySize);

    ...
}

void GameEngine::loadData(Army& p1, Army& p2, Battlefield& testField, RuleSet& rs, int& xSize, int& ySize)
{
    string terrain = BattlefieldUtils::pickTerrain();
    string armySplit[14];//id index 1
    string ruleSplit[19];//in index 7
    string armyP1, armyP2, ruleSet;
    Skill p1Skills[8];
    Skill p2Skills[8];
    CreatureStack p1Stacks[20];
    CreatureStack p2Stacks[20];

    ...
}

CreatureStack(){quantity = 0; isLive = false; id = -1;};

Army(){};

Battlefield(){};

RuleSet(){};

I have posted every line of code that executes until the program crashes. This code ran fine for a long time, I added some stuff that does not even execute until way after the code I have posted here, and bam, a stack overflow that occurs at GameEngine::loadData() line: CreatureStack p2Stacks[20]; will not go away. What am I doing wrong here? Is that all the stack can handle? I increased the stack size in Visual Studio and got the error to go away, but that slowed things down considerably, so how do I get to the source of the issue and fix that?

3
  • How large are these objects you are storing on the stack? (Like CreatureStack, Battlefield, RuleSet, etc.) Commented Jun 11, 2010 at 3:18
  • 2
    Are you sure you are not somehow calling loadData() (or playGame()) recursively? Commented Jun 11, 2010 at 3:21
  • You should use a debugger and print a stack trace. That will tell you if the problem is infinite recursion (and what the call cycle is) or if the depth of calls is small and the problem is the amount of data stored in the stack itself. Commented Jun 11, 2010 at 7:42

1 Answer 1

4

Clearly, CreatureStack is a large object. You are allocating 20 of them on the stack. Result: stack overflow.

Instead, change to new or malloc for your array of CreatureStack's, moving them into heap memory instead of stack.

Don't forget to free them when done.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd suggest storing your CreatureStacks in a smart pointer, such as boost::shared_ptrso that you don't have to remember to delete them. boost.org/doc/libs/1_43_0/libs/smart_ptr/shared_ptr.htm
Or a std::vector of CreatureStacks might be easier since it'll free itself :)
You should not suggest people to use malloc() for allocating storage for a C++ object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.