Differences between running an executable with Visual Studio debugger vs without debugger

前端 未结 2 1225
忘掉有多难
忘掉有多难 2020-12-02 14:50

I\'m trying to debug an issue in which an executable produces repeatable output (which I want) when executed directly from Visual Studio, but does not produce repea

2条回答
  •  臣服心动
    2020-12-02 15:31

    Well, it is difficult to say without knowing a bit more about your code. However, I had a similar problem with a program doing lots of floating-point arithmetics (double precision numbers).

    The issue would show up when I was dealing with numbers that were slightly different, but numerically indistinguishable for the machine. If two doubles differ by less than numeric_limits::epsilon(), they are seen as the same number for the machine. Hence, expressions of the type:

    if (num1==num2)...
    

    or

    if (num1

    can result in colourful effects.

    These colourful effects can vary when run in debug or release mode. The reason is that debug/release run-time libraries are different. Also, and crucially, the compilation is done with different code optimisations. The difference between the command-line debug version and the debug-window version (F5) is also explained by subtle optimisation differences.

    If you're using VS, you can have a look at the effect of the different compilation options and optimisations in the C/C++ and Linker section of the Properties menu.

    To avoid this problem, I recommend using the numeric_limits facilities from the STL library. As an example, the implementation of a less-than operator should be something like this:

    bool operator<(double num1, double num2) {
        double difference=fabs(num1-num2);
        if (difference>numeric_limits::epsilon()) {
            if (num1 < num2) return true;
            return false;
        }
        return false;
    }
    

提交回复
热议问题