非常奇怪的行为发生在我身上。我在 Windows 7 64 位上分别使用最新的 Cygwin32、Cygwin64 和 MinGW32 以及 GCC 4.9.2、4.9.2 和 4.8.1。我也在使用 GCC 4.8.2 在 32 位 Linux 上进行测试。
所以在所有系统上这都有效
#include <bits/stdc++.h>
using namespace std;
string s,t;
int main(){
cin>>s>>t;
cout<<s;
}
这行得通
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
string s="a",t="b";
int main(){
cin>>s>>t;
cout<<s;
}
但下一个在输入上述 3 个配置的第一个字符串后在 Windows 上崩溃,但在 Linux 上工作正常:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
string s,t;
int main(){
cin>>s>>t;
cout<<s;
}
显然唯一的区别是输入前字符串为空,并且启用了_GLIBCXX_DEBUG
。
首先,这是一个可重现的问题吗?也就是说,它是发生在每台配置相同的电脑上,还是只发生在我的电脑上?第二,问题是什么?我应该怎么办?我显然需要 _GLIBCXX_DEBUG
来调试代码中的其他 STL 结构。
最佳答案
_GLIBCXX_DEBUG
更改 STL 类的 ABI(即结构布局,包括流和 std::string
):
To use the libstdc++ debug mode, compile your application with the compiler
flag -D_GLIBCXX_DEBUG. Note that this flag changes the sizes and behavior of
standard class templates such as std::vector, and therefore you can only link
code compiled with debug mode and code compiled without debug mode if no
instantiation of a container is passed between the two translation units.
(来自 official docs )。
您的应用(使用 _GLIBCXX_DEBUG
编译)将 std::string
传递给 中的
(在没有这个宏的情况下编译)因此违反了上述要求。此类违规通常表现为运行时崩溃:参见 this或 this具体例子。std::cin::operator >>
libstdc++.dll
关于c++ - cin 在 Windows 上不能使用空字符串 when_GLIBCXX_DEBUG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28708802/