简体   繁体   English

为什么这个 if/else 语句似乎被优化掉了?

[英]Why does this if/else statement appear to be optimized away?

This is the code in question:这是有问题的代码:

    void DeckTug::StickCallback(unsigned long long evtID, DWORD value)
{
    long int val = value;


    if (evtID == stickXInputID || evtID == stickAxisXInputID)
        stickXpct = (((double)val)) / 325.94;
    else if (evtID == stickYInputID || evtID == stickAxisYInputID) {
        stickYpct = (((double)val)) / 325.94;
        if(isAuto)
        if ((stickYpct < 0.0)) {
            acPullingTug = true;
            tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
        }
        else {
            acPullingTug = false;
            tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
        }
    }
}

When I compile a debug build, this runs perfectly.当我编译一个调试版本时,它运行得很好。 When I compile a release build, it does not work.当我编译发布版本时,它不起作用。 When I attach the visual studio debugger to the release version, I can break on the first if statement and on the closing brace of the function, but I cannot hit a break point anywhere else, and neither stickXpct or stickYpct are ever being assigned anything, although in the debugger I can see that "value" has a valid value, and "evtID" DOES equal one of inputIDs.当我将 Visual Studio 调试器附加到发布版本时,我可以在第一个 if 语句和函数的右大括号处中断,但我无法在其他任何地方遇到断点,并且 stickXpct 或 stickYpct 都没有被分配任何东西,尽管在调试器中我可以看到“value”具有有效值,而“evtID”确实等于 inputID 之一。 In conclusion, it looks to me like, in the release version of the code only, both the first "if" statement and the first "else if" statement only evaluate to false, even when one of them should evaluate to true.总之,在我看来,仅在代码的发布版本中,第一个“if”语句和第一个“else if”语句都只评估为假,即使其中一个应该评估为真。 Does anyone know what is going on here?有谁知道这里发生了什么? because I don't.因为我没有。

Thanks so much, Farley非常感谢,法利

Edit: changed answer in response to comments编辑:根据评论更改答案

Try adding volatility尝试增加波动性

void DeckTug::StickCallback(unsigned long long evtID, DWORD value)
{
    long int val = value;
    volatile unsigned long long _evtID = evtID;


    if (_evtID == stickXInputID || _evtID == stickAxisXInputID)
        stickXpct = (((double)val)) / 325.94;
    else if (_evtID == stickYInputID || _evtID == stickAxisYInputID) {
        stickYpct = (((double)val)) / 325.94;
        if(isAuto)
            if ((stickYpct < 0.0)) {
                acPullingTug = true;
                tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
            }
            else {
                acPullingTug = false;
                tugTBoffset = tugReversed ? towbarAttachAft * (-1.0) : towbarAttachForward;
            }
    }
}

That should prevent the compiler from optimizing those branches until you can track down why it wants to optimize those branches away.这应该会阻止编译器优化这些分支,直到您可以找出它为什么优化这些分支为止。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM