繁体   English   中英

为什么打印出来的元组中只有一个可以工作,但是打印出来就可以了?

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

我无法理解为什么我不使用debug来打印输出时返回的数据是垃圾,而当我将其打印出来时返回的数据却很好。 我正在使用C ++ make_tuple并在另一端绑定浮点值。 如果我没有提供足够的信息,请告诉我!

我尝试通过打印出我的函数来检查未初始化的数据。 我在程序的其他部分也使用了完全相同的代码,没有任何问题。

给出该程序是什么的背景。 我正在读取获取最大值的adc值(带有错误检查),然后将其发送给系统以通过失败并显示给用户。 我可以通过几种方法来解决此问题,但我大多只是对这个错误感到好奇。

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));

为什么这让我非常烦恼,是因为每次我使用qDebug()函数将其打印出来时,它都能正常工作! 如果我将其注释掉,它可以回到3.8581 * 10 ^ -38

该值神奇地恢复为正确的值。

这里发生了什么? 我的猜测是make_tuple和tie正在破坏内存,但如果是这样,为什么它只是偶尔执行呢? 为什么只有一个浮标?

解决了

我的采样超出了初始化数组的范围。 我的数组设置为“ HV_SAMPLES”,但是最大循环数为500,因此它的采样超出了我的数组的大小。 调试功能必须在数组和其他值之间增加一些缓冲,以使其能够正确输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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