Consider the following method which reads a line from a text file and tokenizes it:
std::pair<int, int> METISParser::getHeader() {
// handle header line
int n; // number of nodes
int m; // number of edges
std::string line = "";
assert (this->graphFile);
if (std::getline(this->graphFile, line)) {
std::vector<node> tokens = parseLine(line);
n = tokens[0];
m = tokens[1];
return std::make_pair(n, m);
} else {
ERROR("getline not successful");
}
}
A crash happens in std::getline (pointer being freed was not allocated – won’t go into the details here). The crash does not happen if I compile my code on other systems and is very likely not an error in my own code. For the moment I am unable to fix this, and I don’t have the time, so I’ll just try to bypass it with your help:
Can you suggest an alternative implementation that does not use std::getline?
EDIT: I am on Mac OS X 10.8 with gcc-4.7.2. I tried in on SuSE Linux 12.2 with gcc-4.7, where the crash does not happen.
EDIT: One guess was that parseLine corrupts the string. Here is the code for completeness:
static std::vector<node> parseLine(std::string line) {
std::stringstream stream(line);
std::string token;
char delim = ' ';
std::vector<node> adjacencies;
// split string and push adjacent nodes
while (std::getline(stream, token, delim)) {
node v = atoi(token.c_str());
adjacencies.push_back(v);
}
return adjacencies;
}
You can always write your own slower and simpler
getline, just to make it work: