简体   繁体   中英

Why does only one of my return tuple work but is fine when I print it out?

I'm having trouble understanding why my return data is garbage when I don't use debug to print it out and is fine when I do print it out. I am using C++ make_tuple and tie on the other end for float values. If I don't include enough info let me know!

I have tried checking for uninitialized data by printing out my functions. I also use this exact same code in other parts of the program with no issue.

To give a background of what this program is. I am reading an adc value getting the max value (with error checking) and then sending it for a pass-fail for the system and display to the user. I can work around this in a few ways but I am mostly just curious about this bug.

std::tuple<float,float> hardware_control::hv_check()
{
    float hv_filtered_max = 0;
    float hv_filtered_avg = 0;

    int samples = HV_SAMPLES;
    float hv_adc_read[samples];
    int non_zero_samples = 0;
    int i = 0;
    int loops = 0;

    //here we will take the a number of samples, average and find the max
    while((i < samples) && (hv_filtered_max < HV_Voltage_MAX_CHECK)) // check if less than HV_MIN to speed up test (basically stop testing if it has passed the check)
    {
        hv_adc_read[i] = check_adc(7);

        if(hv_adc_read[i] > 0 && hv_adc_read[i] < 10)
        {
          hv_filtered_avg += hv_adc_read[i];
          non_zero_samples++;
          i++;
        }

        if((hv_adc_read[i] > hv_filtered_max) && hv_adc_read[i] < 10)
        {
            hv_filtered_max = hv_adc_read[i];

        }

        loops++;

        if(loops > 500) // stop sampling at 500 if we never get anything (this is protection for it possibly freezing i we sample nothing)
        {
            hv_filtered_max = 0;
            break;
        }
    }
    hv_filtered_avg = hv_filtered_avg/non_zero_samples;

return std::make_tuple(hv_filtered_avg,hv_filtered_max);
}

        hardware_control hwc;

        //where I call and return the data
        std::tie(Ins_Data.hv_avg,Ins_Data.hv_max) = hwc.hv_check();

        //Me printing out the values
        qDebug()<<"MAX_OUTSIDE"<<Ins_Data.hv_max<<endl;

        Ins_Data.hv_errors  = hwc.HV_error_check();

        log_data.push_back("HV_AVG");
        log_data.push_back(QString::number(Ins_Data.hv_avg*3));
        log_data.push_back("HV_MAX");
        log_data.push_back(QString::number(Ins_Data.hv_max*3));

Why this annoys me so bad is that every time I print it out with the qDebug() function it works! if I comment it out, it goes back to 3.8581*10^-38

The value magically comes back to the correct value.

What's going on here? My guess is the make_tuple and tie is corrupting the memory but if so then why is it only sporadically doing it? and why only one of the floats?

SOLVED

I was sampling beyond my initialized array. My array is set to "HV_SAMPLES" however the max number of loops was 500, therefore it sampled beyond the size of my array. Debug functionality must have added some cushion between the array and other values allowing it to output correctly.

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