c++ - In what case can this if-statement throw an exception? -
apparently, when code reaches if
statement, string subscript out of range
exception occurs.
// int // str std::string while ( true ) { // other stuff if( == str.size() ) // line throws exception break; }
in case can such simple if-statement throw exception? don't see it. shouldn't return 0
if reason comparison fails?
edit: full function in occurs. reads through file , takes in value of tokens of its. , if of relevancy, i'm using visual studio 2010 express , error says "debug assertion failed".
void function(string &str, int start) { int outline; // read attributes int pos, pos2 = start; while( true ) { pos = skipwhitespace(str, pos2); pos2 = findendoftoken(str, pos); string token = str.substr(pos, pos2-pos); pos = skipwhitespace(str, pos2); if( pos == str.size() || str[pos] != '=' ) break; pos = skipwhitespace(str, pos+1); pos2 = findendoftoken(str, pos); file<<"...part 3"; string value = str.substr(pos, pos2-pos); if( token == "outline" ) outline = (short)strtol(value.c_str(), 0, 10); if( pos == str.size() ) // <--- error here (at least, seems so) break; } setoutline(outline); }
and skipwhitespace()
, findendoftoken()
functions these two.
int skipwhitespace(string &str, int start) { uint n = start; while( n < str.size() ) { char ch = str[n]; if( ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n' ) break; ++n; } return n; } int findendoftoken(string &str, int start) { uint n = start; if( str[n] == '"' ) { n++; while( n < str.size() ) { char ch = str[n]; if( ch == '"' ) { // include last quote char in token ++n; break; } ++n; } } else { while( n < str.size() ) { char ch = str[n]; if( ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '=' ) break; ++n; } } return n; }
this line can never throw exception. sure real c++ exception , not crash?
Comments
Post a Comment