views:

49

answers:

2

Hi, 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.

alt text

#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 ?

+1  A: 

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.

Steve Townsend
+7  A: 

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.

Hans Passant
+1 - the case where I've seen this used is where the code corresponding to metacode input to a C++ parser generator contains references back to the original line numbers in the metacode.
Steve Townsend