C++堆栈溢出
这是一些代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,CreatureStack 是一个大对象。
您将在堆栈上分配其中的 20 个。。结果:堆栈溢出。
相反,将 CreatureStack 数组更改为
new
或malloc
,将它们移动到堆内存而不是堆栈中。完成后不要忘记释放它们。
Clearly, CreatureStack is a large object.
You are allocating 20 of them on the stack. Result: stack overflow.
Instead, change to
new
ormalloc
for your array of CreatureStack's, moving them into heap memory instead of stack.Don't forget to free them when done.