简体   繁体   English

使用#line控件时调试器的奇怪行为

[英]Strange behavior of debugger when #line control is used

I used below code and tried to debug in Visual studio 2008 by pressing F10. 我使用下面的代码并尝试通过按F10在Visual Studio 2008中进行调试。

//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. #line 100告诉编译器转到第100行以获取其下一行。 As 100th line doesn't exist, it goes outside the main function as shown in the screenshot. 由于第100行不存在,它将超出主要功能,如屏幕截图所示。 If i try to debug code with F10, control never comes back to main function. 如果我尝试用F10调试代码,控制永远不会回到主函数。 It keeps on showing the pointer outside the main function even though it is executing main(). 它继续显示主函数外部的指针,即使它正在执行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 如果我给其他文件名代替test.cpp,指针将转到该文件,但它不会返回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. Gigo,垃圾进,垃圾出来。 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. 对于C# ,提到了从调试器中隐藏代码行,但这仍然不会改变预期的控制流。

In both languages you would have to alter execution flow manually using "Set Next Statement" after selecting the required next line of code. 在两种语言中,您必须在选择所需的下一行代码后使用“Set Next Statement”手动更改执行流程。 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. 设置要在当前范围之外执行的下一行代码可能会导致程序出现故障 - 引用的文档中还有其他警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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