简体   繁体   English

程序可以在Debug中正常运行,但不能在Release中正常运行

[英]Program works fine in Debug, but not in Release

I made a tic-tac-toe game which works good. 我做了一个井字游戏,效果很好。 Recently I decided to add a reset game feature. 最近,我决定添加一个重置游戏功能。 Here is the part where I take input from user 这是我接受用户输入的部分

void playerMove() //Gets the player move and updates the box variables
{
    int boxnumber;
    while(1)      //Set's loop if the player enters a invalid input
    {
       cout << "\n Player " << player << "'s turn!(Press 0 to reset the game!): ";
       cin >> boxnumber;
       if(box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0) // Checks if the input is valid or not
       {
           system("CLS");//If invalid, show error and loop back to start to get new input
           displayBoard();
           cout << "\n Invalid Input! Try again!\n";

       }else if(boxnumber == 0)
       {
           refreshGame();
           displayBoard();
       }else
       {
           box[boxnumber-1] = player;//If inputs are fine, set the box variable's and break out of loop!
           break;
       }
    }

}

Now, in debug mode, when I press 0, everything runs fine and the game is reset, but in release build, when I press 0, it gives me the "Invalid Input! Try again!" 现在,在调试模式下,当我按0时,一切运行良好,并且游戏已重置,但是在发布版本中,当我按0时,它给了我“无效输入!请重试!”

Things I have tried the didnt work: -Rebuild the entire release and debug version. 我尝试过的没用的东西:-重建整个发行和调试版本。 -Making a new project and copy-pasting my code. -制作一个新项目并复制粘贴我的代码。 Same thing, debug works, release doesnt. 同样的事情,调试工作,发布没有。

For anyone wondering, I am using code::blocks IDE. 对于任何想知道的人,我正在使用code :: blocks IDE。 The compiler is GNU GCC. 编译器是GNU GCC。 Thanks for the help! 谢谢您的帮助! :) :)

You have undefined behavior . 您有未定义的行为

In the "first" if you have box[boxnumber-1] , so when you enter 0 (as you have stated in your question), you're trying to access element with index -1 . 在“ first”中, if您具有box[boxnumber-1] ,那么当您输入0 (如您在问题中所述)时,您正在尝试访问索引为-1元素。 That's UB, as you're reading "invalid" memory. 那是UB,因为您正在读取“无效”内存。

You should check for 0 first (and for negative numbers also). 您应该先检查0 (也要检查负数)。

In the if statement, put the range checks in front of the value checks. if语句中,将范围检查放在值检查的前面。 That is, change 也就是说,改变

if(box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0)

to

if(boxnumber < 0 || box number > 9 || box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O')

That takes advantage of short-circuiting: if the input value is less than 0 or greater than 9 the value checks won't be executed. 这利用了短路的优势:如果输入值小于0或大于9,则将不执行值检查。 That will avoid checking things like box[10] , which isn't valid. 这样可以避免检查box[10]无效内容。

That still leaves a problem: if the user inputs 0, this code will happily check box[-1] , which is also out of range. 仍然存在一个问题:如果用户输入0,则此代码将愉快地选中box[-1] ,这也超出了范围。 To get rid of this, move the branch of the if statement that tests box number == 0 in front of this part: 为了消除这一点,请将if语句的分支移动到该部分的前面,该语句测试box number == 0

if (box number == 0)
    // whatever
else if (box[boxnumber-1] == 'X' || box[boxnumber-1] == 'O' || boxnumber > 9 || boxnumber < 0)
    // whatever else

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 程序在调试版本中运行良好,但在发布版本中失败 - Program works fine in Debug build, but fails in Release build 问答程序在发布版本时崩溃,在调试版本中运行良好 - Question and Answer program crashes on release build, works fine on debug build 调试模式下未处理的异常,但在发行版中工作正常 - Unhandled exception in debug mode but works fine in release 在发布模式下OpenAL Soft崩溃(调试工作正常) - OpenAL Soft crashes in release mode (debug works fine) 在Debug配置中进行编译可以正常工作,但是Release会给出链接错误 - Compiling in Debug configuration works fine, but Release gives link errors 为什么在发布模式下不能访问 for 循环,但在调试中它工作正常 - Why isn´t the for loop accessed in release mode, but in debug it works fine CImg在调试模式下引发异常,在Release中运行良好 - CImg throws an exception in Debug mode, works fine in Release Mongodb Cxx驱动程序测试在发行版中崩溃[在调试中工作正常] - Mongodb Cxx Driver Test crashing in Release Build [ Works fine in Debug ] 自定义 memory 管理器在发布模式下工作正常,但在调试模式下无法正常工作 - Custom memory manager works fine in release mode, but not in debug mode SFML 2.1程序在调试模式下运行良好,但在发布模式下崩溃 - SFML 2.1 program runs fine in debug mode, but crashes in release mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM