Hi all,

I am coming accross a problem that says "C4703: potentially uninitialized local pointer variable 'data_list' used" and "C4703: potentially uninitialized local pointer variable 'filter_list' used". I have tried initilizing the pointer like I have read while googling it but have not managed to get it to work as even after trying to initialize it it still comes accross with an error.
This is my code that is giving me problems.
Thanks

void TheMenu::Load(TheFilter& Filter, TheData& Data)
{
    fstream input_file;

    input_file.open("task2_data.txt"); //open the file

    if (!input_file)
    {
        cout<<"\nFile does not exist" << endl;
        //exit(0);
    }
    else
    {/////////////////////////////////////////////////////////
        int data_count = -1;
        int data_size = -1;
        int filter_count = -1;
        int filter_size = -1;
        double* data_list;
        double* filter_list;


        while (!input_file.eof()) //while we haven't reached the end of the file
        {
            string str;
            getline(input_file,str); //get the current line in the file (separated by \n)  - get from input_file, store it in the string str

            if (data_count == -1) {// 1st time entering loop get size of data array.
                data_size = atoi(str.c_str());//convert string to integer
                //Data.SetData(new double[data_count]);
                data_list = new double[data_size];
            }

            if ((data_count != -1) && (data_count < data_size))
            {   
                double val = atof(str.c_str()); // string float, double type used in other classes
                data_list[data_count] = val; // data list @ position 

                if(data_count+1 == data_size)
                {
                    Data.SetData(data_list);
                    Data.SetLength(data_size);
                    Data.SetValid(true);
                    cout << "data loaded" <<endl;
                    filter_count = 0;   // info for next loop 
                    data_count = data_size; // match position to number of vales, if true begin saving to class 
                }
            }
            else if (filter_count != -1)
            {
                if (filter_size == -1) {
                    filter_size = atoi(str.c_str());
                    filter_list = new double[filter_size];
                    filter_count--;
                }

                else if ((filter_count != -1) && (filter_count < filter_size))
                {
                    double val = atof(str.c_str());
                    filter_list[filter_count] = val;

                    if(filter_count+1 == filter_size)
                    {
                        Filter.SetValues(filter_list);
                        Filter.SetLength(filter_size);
                        Filter.SetValid(true);
                        cout << "filter loaded" <<endl;
                    }
                }
                filter_count++;
            }

            data_count++;   
        }


    }
}

I think you can remove the warning by initializing those two pointers to NULL when they are declared on lines 18 and 19.

Since this is C++, use 0 to initialze these variables. IE:

double* data_list = 0;
double* filter_list = 0;

You only declared your pointer. Also you allocate memory for your filter_list pointer in an if clause, yet you try to access that pointer in an else if clause.

The compiler throws a warning, saying that it's possible to enter only in the else if clause, without entering first in the if clause, thus getting in a situation where your pointer is not initialized, and you want to use it.

double* filter_list;
//...

if (filter_size == -1) {
    //...
    filter_list = new double[filter_size]; //allocation
    //...
}
else if ((filter_count != -1) && (filter_count < filter_size))
{
    //...
    filter_list[filter_count] = val; //using the variable
    //...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.