C++ custom string trim implementation valgrind warning -
recently implemented custom function trimming std::strings removes whitespace character prefixes , suffixes.
i tested functionality , works according unit tests, when run tests using valgrind, following output:
==4486== conditional jump or move depends on uninitialised value(s) ==4486== @ 0x415dda: is_ws_char(char) (parse.cpp:22) ==4486== 0x415bc6: parse::trim(std::string&) (parse.cpp:34)
my input test string was
string s(" a");
i not see problem here. code looks this:
inline bool is_ws_char(const char c) { // line 22 in code return (c == ' ' || c == '\n' || c == '\t' || c == '\r'); } void parse::trim(std::string& str) { size_t len = str.size(); size_t = 0; (; < len; ++i) if (!is_ws_char(str[i])) break; const size_t start = i; (i = len - 1; >= 0; --i) if (!is_ws_char(str[i])) // line 34 in code break; const size_t end = i; str = str.substr(start, end - start + 1); }
does has idea problem here? briefly thought is's valgrind oddity, seems rather unlikely.
thanks in advance!
this loop invalid
for (i = len - 1; >= 0; --i)
the condition equal true because expression --i >= 0 due fact unsigned integer.
also when str.size() equal 0 len - 1 equal std::string::npos.
Comments
Post a Comment