If statement weirdness in Visual Studio 2008

前端 未结 6 1004
梦如初夏
梦如初夏 2020-12-30 08:09

I\'ve come across a problem so strange, that I\'ve recorded my session because I didn\'t think anyone would belive me.

I came across a bug that seems to be at very f

6条回答
  •  太阳男子
    2020-12-30 08:39

    I've seen a similar case a long time ago, in Delphi, so my question is this: Are you compiling for Release or Debug, with or without optimizations?

    The reason I'm asking is that once, during a debug session, I discovered a small procedure that consisted of 4-5 lines of code that, according to the debugger, appeared to be executing in reverse.

    Basically, with the following type of code:

    procedure Test;
    begin
        Line1;
        Line2;
        Line3;
        line4;
    end;
    

    The execution order, according to the debugger, was this:

    procedure Test;
    begin             start -+
        Line1;               |                             +-> here -+
        Line2;               |                   +-> here -+         |
        Line3;               |         +-> here -+                   |
        line4;               +-> here -+                             |
    end;                                                             +-> end
    

    The reason was that the lines was side-effect free in between themselves, so the compiler "optimized" the code by rewriting it, in effect rearranging the code to appear to execute fully in reverse.

    So, do you have a throw statement further down that is actually the one getting executed, but the compiler shows this as the one you have problems with, because, due to rearranging the code, two throw-statements are actually only emitted once as executable code?

    Note: I do not have any reason to know that this is what Visual Studio is doing, but this was what came to my mind when seeing your video.

提交回复
热议问题