![](/img/trans.png)
[英]How to get information from a namespace polluted memory dump, using windbg "dt" command
[英]How to find out how namespace got polluted?
考虑以下一小段代码:
// all of these include other headers, lots of code:
#include "myheader1.h"
#include "myheader2.h"
#include <string>
void foo() {
string s("hello world"); // oh no, why does this compile??
}
这个编译,显然一些递归包含的头文件有一个
using namespace std;
某处。 您将如何找到违规代码行的位置?
在所有头文件上使用grep
实际上不会起作用,因为该语句通常在函数内部使用,它是安全的并且不会污染其余的代码。
无论如何,grep可能会有用。 搜索“^ using namespace”。 在一个函数里面有一个非常公平的机会它会缩进,但在它之外它不会......
编译器通常必须给预处理输出(-E是常见),该得到亦是如此源极线的指示在q上的可能性#line
线。
试着把string s;
在每个#include
语句之后找到它不会导致错误的第一个地方。 这将显示导致问题的标头。 然后对该标题内的#include
语句执行相同操作,依此类推。
一点手动过程,但不应该花太长时间。
$ g++ -E souce.cpp | less
从较少的类型/using
,然后从那里寻找前一行看起来像
# <file_name> <line_number>
这就是预处理器如何告诉编译器某些代码来自哪个源,因此它可以正确地执行错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.