简体   繁体   中英

Vector Iterator (Segmentation fault)

I've designed a program to read in data from a CSV file containing information from a live updating meteorological centre.

I am now in the process of splitting up certain duties into functions, I'm required to print the average windspeed & radiation for each month of a specified year. The sample input: {Year} 2016

Sample output: {Month, Avg Windspeed, Avg Radiation} January, 5.5, 196.4 February, 4.4, 200.3 ... etc.

My program is setup as a vector of "WindData", which contains different classes such as 'Date', 'Time' & windspeed data.

This following function is the one i'm having issues with:

void averageWind(int yearNum, std::vector<WindData>& windlog)
{
    std::string month[12] = {"January", "February", "March", "April", "May", "June",
                            "July", "September", "October", "November", "December"};
    int monthCount = 0, monthNum[12] = {0}, monthAverage[12] = {0};
    int dayCount[12] = {0}, totalWindSpeed[12] = {0}, totalRadiation[12] = {0}, mWindAverage[12] = {0};

    std::vector<WindData>::iterator iter;
    for(iter = windlog.begin(); iter != windlog.end(); iter++)
    {
        if(iter->getYear() == yearNum)
        {
            for(int i = 0;  i < 12; i++)
            {
                while((iter->getMonth()-1) == i)
                {
                    dayCount[i]++;
                    totalWindSpeed[i] += iter->getSpeed();
                }
                std::cout << "Wind Speed: " << totalWindSpeed[i] << std::endl;
                std::cout << "Day Count: " << dayCount[i] << std::endl;
                mWindAverage[i] = (totalWindSpeed[i] / dayCount[i]);

                //std::cout << mWindAverage[i];
                //std::cout << month[i];
            }

        }


    }


}

This is obviously not complete and the print methods are there just for testing purposes, my issue is that totalWindSpeed and dayCount are both producing 0. I've attempted to debug the code to determine where it is going wrong and it seems that there is a problem with the iterator itself, however this worked previously when i had it within my main function.

The error is as follows:

Program received signal SIGSEGV, Segmentation fault.

What could be causing this? I've read up about deferencing the iterator etc. but from what I can tell it's setup right.

Without additional information I would do something like this.

for (WindData& windData : windlog) {
    if (windData.getYear() == yearNum) {
        int i = windData.getMonth() - 1;
        totalWindSpeed[i] += windData.getSpeed();
        dayCount[i]++;
    }
}

for (int i = 0; i < 12; i++) {
    // If each month appears only once, this may go into the previous loop.
    mWindAverate[i] = totalWindSpeed[i] / dayCount[i];
}

Your first problem is here:

mWindAverage[i] = (totalWindSpeed[i] / dayCount[i]);

For the most values of i dayCount[i] ist zero. I expect your segmentation fault comes from here. You should test first the value to divide with is not zero.

The second problem is this

for(int i = 0;  i < 12; i++)

You do not have to iterate through the months again. You only have to add to the right month when you read the data with the iterator. See the other answer above (does not test for division with 0 either !!). Your code becomes suddelny needless complicated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM