0

I have a problem with my C++ code as valgrind is giving me the error (Conditional jump or move depends on uninitialized value(s)) at lines highlighted below. I tried initializing "type" with an empty string, but doesn't seems to be working. Any help here much appreciated.

Many thanks in advance.

nandanator

int AB_CD::checkType(string & str)
{
    int tmp = 0;

    string type;


    while (getNextSubstring(str, type))
    {
        if (type == "AAA")     <<<<< ----- Valgrind error pointing here 
        {
            tmp |= static_cast<int>(TYPE_AAA);
        }
        else if (type == "BBB") <<<<< ----- Valgrind error pointing here 
        {
            tmp |= static_cast<int>(TYPE_BBB);
        }
        else if (type == "CCC") <<<<< ----- Valgrind error pointing here 
        {
            tmp |= static_cast<int>(TYPE_CCC);
        }
        else
    {
        //Print a warning
    }

    }

    return tmp;

}



bool AB_CD::getNextSubstring(string & input, string & substring)
{
    bool found_substring = false;

    string::size_type start = input.find_first_not_of(" ,");
    string::size_type end = input.find_first_of(" ,", start);

    if (start != string::npos)
    {
        substring = input.substr(start, end-start);
        input.erase(start, end-start);
        found_substring = true;
    }
    return found_substring;
}
nandanator
  • 421
  • 1
  • 4
  • 5
  • AB_CD::getNextSubstring() must return true or the while() loop would not be entered. That means `substring = input.substr(start, end-start);` hapens and must be setting your type string to something that is uninitialized. Note: I recommend changing your API for getNextSubstring(). Maybe make it `string getNextSubstring(const string & input);` and throw an exception when nothing is found. – Goswin von Brederlow Jul 23 '15 at 13:24
  • @GoswinvonBrederlow Thank you. :). I shall try this out and get back. – nandanator Jul 23 '15 at 15:53
  • New snippet looks like below. `string AB_CD::getNextSubstring1(string & input) { string substring = "" ; substring.empty(); string::size_type start = input.find_first_not_of(" ,"); string::size_type end = input.find_first_of(" ,", start); if (start != string::npos) { substring = input.substr(start, end-start); input.erase(start, end-start); } return substring; }` .. `type = getNextSubstring1(str);` ... Still I am getting the error. `if (type == "AAA") << Here { tmp |= static_cast(TYPE_AAA); }` – nandanator Jul 24 '15 at 12:14
  • @GoswinvonBrederlow Could you please comment? – nandanator Jul 29 '15 at 08:29
  • You didn't check or fix the 'input.substr(start, end-start)' that is probably corrupting your string – Goswin von Brederlow Mar 05 '16 at 20:31

0 Answers0