我的 int main() 中出现堆栈溢出错误

I got a stack overflow error in my int main()

本文关键字:堆栈 栈溢出 错误 int main 我的      更新时间:2023-10-16

我正在写一个贪吃蛇游戏,int main()我遇到了堆栈溢出,我不知道如何解决它。 而且我真的不知道如何使堆变量:P。错误读取严重性代码说明项目文件行抑制状态 警告 C6262 函数使用堆栈的"17444"字节:超过/analyze:堆栈大小"16384"。 考虑将一些数据移动到堆中。

这是我的主要:

int main()
{
vector<point> snek;
mt19937 mt_rand(time(0));
snek.push_back({ 10,10 });
snek.push_back({ 10,9 });
snek.push_back({ 10,8 });
snek.push_back({ 10,7 });
int highscore = 0, score;
bool tutorial = false;
bool p = false;
char move = 'w';
char sure = 'f';
string board[21][21];
int direction = 2;
point apple;
apple.x = (mt_rand() % 19) + 1;
apple.y = (mt_rand() % 19) + 1;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
board[i][j] = "  ";
}
}
bool loss = false;
while (true)
{
score = snek.size() - 4;
bool  appleEaten = false;
if (snek[0].x == 0)
loss = true;
if (snek[0].x == 20)
loss = true;
if (snek[0].y == 0)
loss = true;
if (snek[0].y == 20)
loss = true;
if (loss)
{
system("CLS");
if (score > highscore)
{
highscore = score;
}
cout << "You lost with a score of " << snek.size() - 4 << endl;
cout << "Your highscore for this session is " << highscore << endl;
cout << "Press any key to play again" << endl;
cout << "Press RMB to quit" << endl;
while (true)
{
if (GetAsyncKeyState(VK_RBUTTON))
{
system("CLS");
cout << "Are you sure you want to quit? Your highscore for this session will be reset" << endl;
cout << "Press Q to quit and P to play again" << endl;
sure = _getch();
if (sure == 'q' || sure == 'Q')
{
_Exit(0);
}
if (sure == 'p' || sure == 'P')
{
p = true;
}
}
if (_kbhit() || p)
{
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
board[i][j] = "  ";
}
}
snek.clear();
snek.push_back({ 10,10 });
snek.push_back({ 10,9 });
snek.push_back({ 10,8 });
snek.push_back({ 10,7 });
loss = false;
p = false;
break;
}
}
}
Sleep(10);
if (_kbhit())
{
move = _getch();
}
switch (move)
{
case 'w':
loss = goTo({ (snek[0].x - 1),snek[0].y }, snek);
Sleep(10);
break;
case 'a':
loss = goTo({ snek[0].x ,(snek[0].y - 1) }, snek);
Sleep(10);
break;
case 's':
loss = goTo({ (snek[0].x + 1),snek[0].y }, snek);
Sleep(10);
break;
case'd':
loss = goTo({ snek[0].x ,(snek[0].y + 1) }, snek);
Sleep(10);
break;
}
board[apple.x][apple.y] = " 0";
for (int k = 0; k < snek.size() - 1; k++)
{
board[snek[k].x][snek[k].y] = " *";
}
board[snek[snek.size() - 1].x][snek[snek.size() - 1].y] = "  ";
if (apple.x == snek[0].x && apple.y == snek[0].y)
{
snek.push_back({ snek[snek.size() - 1].x + 1,snek[snek.size() - 1].y });
appleEaten = true;
}
if (appleEaten)
{
makeFood(apple, snek);
}
for (int i = 0; i < 20; i++)
{
board[0][i] = "--";
board[20][i] = "--";
}
for (int i = 0; i < 20; i++)
{
board[i][0] = '|';
board[i][20] = '|';
}
if (!tutorial)
{
cout << "You are a snake." << endl;
cout << "Your body looks like this" << endl;
cout << "*****" << endl;
cout << "Move with WASD" << endl;
cout << "If you eat the apples, which look like this " << endl << "0" << endl;
cout << "You get bigger. If you try to eat yourself or run into walls, you lose" << endl;
cout << "Click RMB to begin";
while (true)
{
if (GetAsyncKeyState(VK_RBUTTON))
{
system("CLS");
tutorial = true;
break;
}
}
}
system("CLS");
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 21; j++)
{
cout << board[i][j];
}
cout << endl;
}
cout << "Score: " << score;
}
}

我不确定string board[21][21];但这条线可能是堆栈溢出的根本原因。据我所知,std::string由于短字符串优化,在堆栈中存储大小为15-20字节的静态数组(大小取决于编译器,标准未指定(。

如果是根本原因,那么一种选择是在堆上而不是堆栈上分配数据,因此只需更改行

string board[21][21];

std::vector<std::vector<std::string>> board { 21 , std::vector<std::string>{ 21 } };
string board[21][21];

主要使用您的堆栈。您可以将其定义为全局变量,而不是我希望您的程序有足够的堆栈。