开发者

Strange behavior of debugger when #line control is used

I used below code and tried to debug in Visual studio 2008 by pressing F10.

//test.cpp
#include<iostream>
using namespace std;

int main(void)
{
#line 100 "test.cpp"
   cout<<"Inside main()"<<endl;
   return 0;
}

Below is the debugger screen shot.

Strange behavior of debugger when #line control is used

#line 100 tells compiler to go to line 100 to get its next line. As 100th line doesn't exist, it goes outside the main function as shown in the screenshot. If i try to debug code with F10, control never comes back to main function. It keeps on showing the pointer outside the main function even though it is executing main().

If i give other file name in place of te开发者_开发知识库st.cpp, pointer goes to that file, but it doesn't come back to test.cpp

Any idea why debugger is behaving like this ?


This directive should be used by code generators. Tools that translate from one language to another. So that when you debug that code, the debugger will show the source file of the original language, stepping through the statements of that language. Instead of the (often cryptic) statements in the translated code.

Which is not what you did here, you are giving nonsense information in the directive. And obviously got nonsense results when you debug. Gigo, garbage in, garbage out. Remove the directive.


The directive does not alter the actual flow of control. It alters the output generated by the compiler during compilation. Read the docs - does this explain why you would expect the behaviour described above?

For C# there is reference to hiding lines of code from the debugger but this still does not alter the expected flow of control.

In both languages you would have to alter execution flow manually using "Set Next Statement" after selecting the required next line of code. Setting the next line of code to be executed outside current scope can cause the program to malfunction - there are further caveats in the referenced docs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜