gpt4 book ai didi

c++ - 如何在无限循环的游戏中防止堆栈溢出?

转载 作者:行者123 更新时间:2023-11-28 01:21:45 25 4
gpt4 key购买 nike

我用 C++ 命令行制作了一个俄罗斯方 block 游戏。它实际上工作得很好,直到堆栈溢出。我了解堆栈溢出是什么,但我不知道如何在本应永远进行下去的游戏中防止它发生。为了不分享 700 多行代码,我将把代码简化为基本功能。

代码:

int main(){
//Sets position of first block
//picks random tetris piece for player to control
updateScreenMain();
}
int updateScreenMain(){
//All code related to drawing screen is here
//At the end of it, there are several if statments-
//-that test for if the block hit the ground yet
// If one of those functions is called, example: reseting the
// block to the top of the screen, it will call updateScreenMain()
// again
}

基本上,我想做的是清除堆栈内存。我看到当我的函数调用其他函数时,它们会嵌套直到堆栈溢出。我怎样才能避免这种情况?

最佳答案

有很多方法。这是一个例子:

int main() {
while(updateScreenMain());
}
int updateScreenMain() {
// Just return 0 to stop
}

一般来说,只要避免递归即可。

关于c++ - 如何在无限循环的游戏中防止堆栈溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55880616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com