C++堆栈溢出

发布于 2024-09-05 09:04:03 字数 1542 浏览 9 评论 0原文

这是一些代码:

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(){};

我已经发布了直到程序崩溃为止执行的每一行代码。这段代码运行良好很长一段时间,我添加了一些东西,直到我在这里发布的代码之后才执行,并且bam,在 GameEngine::loadData() 处发生堆栈溢出line: CreatureStack p2Stacks[20]; 不会消失。我在这里做错了什么?这就是堆栈可以处理的全部内容吗?我增加了 Visual Studio 中的堆栈大小,错误消失了,但这大大减慢了速度,那么我如何找到问题的根源并解决它呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2024-09-12 09:04:03

显然,CreatureStack 是一个大对象。
您将在堆栈上分配其中的 20 个。。结果:堆栈溢出。

相反,将 CreatureStack 数组更改为 newmalloc,将它们移动到堆内存而不是堆栈中。

完成后不要忘记释放它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文