c++ - cin 在 Windows 上不能使用空字符串 when_GLIBCXX_DEBUG?

标签 c++ gcc cygwin mingw

非常奇怪的行为发生在我身上。我在 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 传递给 中的 std::cin::operator >> libstdc++.dll(在没有这个宏的情况下编译)因此违反了上述要求。此类违规通常表现为运行时崩溃:参见 thisthis具体例子。

关于c++ - cin 在 Windows 上不能使用空字符串 when_GLIBCXX_DEBUG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28708802/

相关文章:

c++ - 使用文件流从文件读取/写入无符号字符数组

gcc - 拦截 GNU tar 的 openat() 系统调用

c - eclipse C 编译器无法识别//行注释

c++ - 错误 : "numeric_limits" was not declared in this scope

c++ - 比较两个 google::protobuf::Message 对象的最简单方法是什么?

c++ - ARM GCC 交叉编译器 fedora 22

c - 在 Cygwin 中使用 NCurses 扫描 USB 设备的输入

eclipse - 配置 Eclipse 以使用 Cygwin 工具链的 bash 登录 shell

c++ - 编译cyanoboot : No rule to make target

c++ - 从单个 *.cc* 源文件生成多个 *.oct* 文件以将 C 库连接到 Octave