Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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 ?

like image 885
bjskishore123 Avatar asked Sep 18 '10 11:09

bjskishore123


2 Answers

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.

like image 193
Hans Passant Avatar answered Sep 20 '22 10:09

Hans Passant


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.

like image 24
Steve Townsend Avatar answered Sep 23 '22 10:09

Steve Townsend