简体   繁体   中英

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.

替代文字

#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 test.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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