升级到Xcode 3.2和Snow Leopard后,我的调试版本在运行时被破坏并失败.Stringstreams似乎不起作用.它们在发布模式下工作.
我把它缩小为GCC 4.2,OSX SDK 10.6和_GLIBCXX_DEBUG预处理器符号的组合.这些是新Xcode项目的调试配置的默认值.
此代码显示了问题:
#include <iostream>
#include <string>
#include <sstream>
int main (int argc, char * const argv[]) {
std::stringstream stream;
std::cout << " expected actual" << std::endl;
std::cout << "stream.bad: 0 " << stream.bad() << std::endl;
std::cout << "stream.fail: 0 " << stream.fail() << std::endl;
std::cout << "stream.eof: 0 " << stream.eof() << std::endl;
std::cout << "stream.good: 1 " << stream.good() << std::endl;
stream.exceptions(std::ios::badbit | std::ios::failbit | std::ios::eofbit);
try{
stream << 11; //< Does not …Run Code Online (Sandbox Code Playgroud) 非常奇怪的行为发生在我身上.我在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;
}
Run Code Online (Sandbox Code Playgroud)
这很有效
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
string s="a",t="b";
int main(){
cin>>s>>t;
cout<<s;
}
Run Code Online (Sandbox Code Playgroud)
但是在上面提到的3种配置中输入第一个字符串后,下一个在Windows上崩溃,但在Linux上正常工作:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
string s,t;
int main(){
cin>>s>>t;
cout<<s;
}
Run Code Online (Sandbox Code Playgroud)
显然,唯一的区别是字符串在输入之前是空的,并且_GLIBCXX_DEBUG已启用.
首先,这是一个可重现的问题吗?即它是在具有相同配置的每台PC上发生的,还是只发生在我的PC上?第二,问题是什么?我该怎么办?我显然需要 _GLIBCXX_DEBUG在代码中调试其他STL结构.