Uninitialized variable - warning

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Wanderer82
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Jul 26, 2016 2:00 pm

Uninitialized variable - warning

Post by Wanderer82 » Fri May 08, 2020 7:57 pm

Hello

My compiler gives me the "variable may be used uninitialized in function" warning. I understand it. I declare a variable in the beginning and then it first gets initialized inside an "if" statement but only if one of the statements is true. So this code opens a file from which then some numbers are read. Depending on the number, some if-path will be taken. If the file was faulty and not a correct number would be read, the code will end without having ever used this uninitialized variable. So actually I will check whether this variable will have a correct value before it is used in the function. Nevertheless I get the warning. Can I ignore it or is there a better solution to improve my code?

Thanks, Thomas

Code: Select all

void readsavedcart(string cart_no, string typ_Warenkorb)
{
    ifstream Cart;
    
    Cart.open(cart_no);
    
    string Info;
    int Prodctnumber;
    int Options_Count;
    int Option1;
    int Option2;
    string Option1_string;
    string Option2_string;
   
    Cart >> Info;

    while (Info == "#")
    {
        Cart >> Info; //get Options_Count
    
        if (Options_Count == 0)
        {
            Option1 = 0;
            Option2 = 0;
            Option1_string = "";
            Option2_string = "";
        }

        if (Options_Count == 1)
        {
            Option1 = string_to_int(Info);
            Cart >> Info;
            Option2 = string_to_int(Info);
            Cart >> Info;
  
            Option1_string = _Product[Productnumber].readOptionen(0, Option1);
            Option2_string = "";
        }

        if (Options_Count == 2)
        {
            Option1 = string_to_int(Info);
            Cart >> Info;
            Option2 = string_to_int(Info);
            Cart >> Info;
            Anzahl = string_to_int(Info);

            Option1_string = _Product[Productnumber].readOptionen(0, Option1);
            Option2_string = _Product[Productnumber].readOptionen(1, Option2);
        }

       if(Options_Count < 0 || Options_Count > 3)
       {
            wxMessageBox("Error reading the file!");
       }

       else
       {
           Warenkorb_count = Warenkorb_count + 1;
           _Warenkorb[Warenkorb_count].setWarenkorb(Productnumber, Option1_string, Option2_string, Option1, Option2);
           Cart >> Info;
        }
   }
   
   Cart.close();
}

User avatar
doublemax
Moderator
Moderator
Posts: 14786
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Uninitialized variable - warning

Post by doublemax » Fri May 08, 2020 10:45 pm

It would have been helpful if you have mentioned the name of the variable in question...

Assuming it's Options_Count, i would say the warning is correct and you should initialize it to zero.

Code: Select all

Cart >> Info; //get Options_Count
I don't see how this line can initialize Options_Count.
Use the source, Luke!

Wanderer82
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Jul 26, 2016 2:00 pm

Re: Uninitialized variable - warning

Post by Wanderer82 » Sat May 09, 2020 9:08 am

Oh my ... Right, I forgot to mention the variable :oops:

Nope, it's Option1 and Option2. As for your suggestion: Yes, in the code I posted it's true, that was a mistake of me shortening the code to make it easier to read. I deleted some more code and also accidentally deleted the line where "Options_Count" gets initialized:

Code: Select all

Cart >> Info;
Options_Count= string_to_int(Info);
But while we're at it. If I initialized "Options_Count" to 0, there would be even more of a mess because if the file isn't read correctly, the value will stay 0 which is wrong and would give a wrong result. So I don't see any sense in initializing variables (and that was my actual question concerning "Option1" and "Option2") that you first want to be sure, that there is a correct value to assign, which is only decided during an if-clause.

User avatar
doublemax
Moderator
Moderator
Posts: 14786
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Uninitialized variable - warning

Post by doublemax » Sat May 09, 2020 10:04 am

In most cases the Uninitialized variable warning indicates a real problem, so you shouldn't ignore it and change the code accordingly.

Here I would initialize those variables at the beginning of the while loop, e.g. to -1 (you have to decide which value makes most sense).

Code: Select all

if(Options_Count < 0 || Options_Count > 3)
If the only valid values are 0-2, the check is wrong (as it also allows 3).
Use the source, Luke!

Wanderer82
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Jul 26, 2016 2:00 pm

Re: Uninitialized variable - warning

Post by Wanderer82 » Sat May 09, 2020 10:29 am

I still don't understand why I should initialize this value. I mean, I can initialize it with basically any value, it doesn't matter. Because this value is only used in the last else-clause (and put into a function) if one of the if-clauses before is true. And only inside these if-clauses the Option1 and Option2 will get a valid value. So just initializing it to any value would lead to a faulty execution of the function anyway.

I guess it's better to just copy the complete code here (this time with German variables). It's also strange that I only get the complaint with "Option1", "Option2" and "Anzahl" but not with the strings "Option1_string" and "Option2_string" as they are - just as the two "Option" variables - only initialized in the if-clause.

Code: Select all

void readsavedcart(string cart_no, string typ_Warenkorb)
{
    ifstream Cart;
    if (cart_no.find('\\') != std::string::npos)
    {
    Cart.open(cart_no);
    }
    else
    Cart.open("C:\\programmieren\\warenkoerbe\\" + cart_no + ".txt");

    string Info;
    int Produktnummer;
    int AnzahlOptionen;
    int Anzahl;
    int Option1;
    int Option2;
    string Option1_string;
    string Option2_string;
    Cart >> Info; //liest Vorname
    Cart >> Info; //liest Nachname
    Cart >> Info; //liest Datum
    Cart >> Info; //liest #

    while (Info == "#")
    {
        Cart >> Info;
        Produktnummer = string_to_int(Info);

        Cart >> Info;
        AnzahlOptionen = string_to_int(Info);

        Cart >> Info;

        if (AnzahlOptionen == 0)
        {
            Anzahl = string_to_int(Info);
            Option1 = 0;
            Option2 = 0;
            Option1_string = "";
            Option2_string = "";
        }

        if (AnzahlOptionen == 1)
        {
            Option1 = string_to_int(Info);
            Cart >> Info;
            Option2 = string_to_int(Info);
            Cart >> Info;
            Anzahl = string_to_int(Info);

            Option1_string = _Produkt[Produktnummer].readOptionen(0, Option1);
            Option2_string = "";
        }

        if (AnzahlOptionen == 2)
        {
            Option1 = string_to_int(Info);
            Cart >> Info;
            Option2 = string_to_int(Info);
            Cart >> Info;
            Anzahl = string_to_int(Info);

            Option1_string = _Produkt[Produktnummer].readOptionen(0, Option1);
            Option2_string = _Produkt[Produktnummer].readOptionen(1, Option2);
        }

        else
        {
            if(AnzahlOptionen < 0 || AnzahlOptionen > 2)
            {
                wxMessageBox("Fehler beim Einlesen des Einkaufswagens!");
            }

            else
            {
                Warenkorb_count = Warenkorb_count + 1;
                _Warenkorb[Warenkorb_count].setWarenkorb(typ_Warenkorb, Produktnummer, Option1_string, Option2_string, Option1, Option2, Anzahl);

                Cart >> Info;
            }
        }
    }
    //Warenkorb_count = 0;
    Cart.close();

}

User avatar
doublemax
Moderator
Moderator
Posts: 14786
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Uninitialized variable - warning

Post by doublemax » Sat May 09, 2020 10:53 am

Code: Select all

        else
        {
            if(AnzahlOptionen < 0 || AnzahlOptionen > 2)
That 'else' here looks wrong and it wasn't there in the first version of the code you posted.

Option1_string is not a primitive variable like an integer, it's a class with a default constructor and it will be initialized to an empty string, so it's not undefined.
I still don't understand why I should initialize this value.
Just trust me (and the compiler) and do it. Otherwise an uninitialized variable issue can and will bite you one day.
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4058
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Uninitialized variable - warning

Post by ONEEYEMAN » Sat May 09, 2020 2:33 pm

Hi,
Besides it is very easy.
If the valid values are assigned only when the file is read correctly, then you know what they are.
Just pick any wrong value and use it to initialize the variable.

Thank you.

Wanderer82
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Jul 26, 2016 2:00 pm

Re: Uninitialized variable - warning

Post by Wanderer82 » Sat May 09, 2020 7:55 pm

Ok ok, I'll just do it :mrgreen:

Post Reply