下面是一些代碼: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()
行的棧溢出:CreatureStack p2Stacks[20];
不會消失。我在這裏做錯了什麼?所有的堆棧都可以處理嗎?我在Visual Studio中增加了堆棧大小,並讓錯誤消失,但是這大大降低了速度,所以我該如何解決問題的根源並解決該問題?
這些對象在堆棧中存儲的大小是多少? (Like CreatureStack,Battlefield,RuleSet等) – 2010-06-11 03:18:27
你確定你不是以某種方式遞歸調用'loadData()'(或'playGame()')嗎? – sth 2010-06-11 03:21:48
您應該使用調試器並打印堆棧跟蹤。這會告訴你,問題是無限遞歸(以及調用週期是什麼),或者調用的深度很小,問題是堆棧本身存儲的數據量。 – 2010-06-11 07:42:30