簡體   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