我在编写一个C++程序(C++ 98)。它会读取一个包含很多行(10000行)的文本文件,这些行是用tab键分隔的值,然后我将其解析成一个Vector of Vector对象。但是,它似乎只能处理一些较小的文件,而其中一个文件却给了我以下错误(该文件有10000行,大小为90MB)。我猜想这是与内存相关的问题?
错误信息
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Abort
代码
void AppManager::go(string customerFile) {
vector<vector<string> > vals = fileReader(customerFile);
for (unsigned int i = 0; i < vals.size();i++){
cout << "New One\n\n";
for (unsigned int j = 0; j < vals[i].size(); j++){
cout << vals[i][j] << endl;
}
cout << "End New One\n\n";
}
}
vector<vector<string> > AppManager::fileReader(string fileName) {
string line;
vector<vector<string> > values;
ifstream inputFile(fileName.c_str());
if (inputFile.is_open()) {
while (getline(inputFile,line)) {
std::istringstream iss(line);
std::string val;
vector<string> tmp;
while(std::getline(iss, val, '\t')) {
tmp.push_back(val);
}
values.push_back(tmp);
}
inputFile.close();
}
else {
throw string("Error reading the file '" + fileName + "'");
}
return values;
}
bad_alloc
被抛出的位置吗?(另外,你有多少可用的内存?) - us2012gdb
,Solaris 也有dbx
。通常,您可以通过在命令行上提供要调试的可执行文件,然后发出run
命令来使用调试器。不过,手册是您的好朋友。 - Kuba hasn't forgotten Monica