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

前端 未结 2 1207
忘掉有多难
忘掉有多难 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<double>::epsilon(), they are seen as the same number for the machine. Hence, expressions of the type:

    if (num1==num2)...
    

    or

    if (num1<num2)...
    ...
    

    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 <limits> 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<double>::epsilon()) {
            if (num1 < num2) return true;
            return false;
        }
        return false;
    }
    
    0 讨论(0)
  • Windows Heap behaves differently if process is started under the debugger. To disable this behavior (in order to find a problem while debugging) add _NO_DEBUG_HEAP=1 to environment (like in this question).

    Alternatively you can attach to process early in program execution. Heap will not enter the debug mode then. Add DebugBreak() line somewhere in the beginning of the execution, run with Ctrl+F5, and start debugging when asked to.

    0 讨论(0)
提交回复
热议问题