简体   繁体   English

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

[英]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. 我无法理解为什么我不使用debug来打印输出时返回的数据是垃圾,而当我将其打印出来时返回的数据却很好。 I am using C++ make_tuple and tie on the other end for float values. 我正在使用C ++ make_tuple并在另一端绑定浮点值。 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. 我正在读取获取最大值的adc值(带有错误检查),然后将其发送给系统以通过失败并显示给用户。 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! 为什么这让我非常烦恼,是因为每次我使用qDebug()函数将其打印出来时,它都能正常工作! if I comment it out, it goes back to 3.8581*10^-38 如果我将其注释掉,它可以回到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? 我的猜测是make_tuple和tie正在破坏内存,但如果是这样,为什么它只是偶尔执行呢? 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. 我的数组设置为“ HV_SAMPLES”,但是最大循环数为500,因此它的采样超出了我的数组的大小。 Debug functionality must have added some cushion between the array and other values allowing it to output correctly. 调试功能必须在数组和其他值之间增加一些缓冲,以使其能够正确输出。

暂无
暂无

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

相关问题 为什么我的变量 sannolikhet 只打印出零? - Why does my variable sannolikhet only print out zero? 为什么我的代码只能正常运行一次? - Why does my code only work properly one time? 为什么我的计数器在 function 应该为 0 时不能正常工作,但其他值 &gt;0 工作正常? - Why does my counter not function properly when it should be 0, but other values >0 work fine? 即使我没有提到数组的大小,为什么在编译和运行代码时仍能正常工作? - Why does the code work fine when I compile and run it even though I have not mentioned size of array? 为什么 printf 在打印十六进制时不只打印出一个字节? - Why does printf not print out just one byte when printing hex? 为什么这个 C++ 代码打印出长度为 5 并且当我打印出字符串时程序会自动终止? - Why does this c++ code print out length 5 and when i'm print out string the program is automatic terminate? 为什么我的向量打印出所有零? - Why does my vector print out all zeroes? 为什么我只能从可变参数模板中提取一个值? - Why am I only extracting one value out of my variadic template? 为什么我的输出流seg错误并且我的虚拟析构函数不起作用,但是当我杀死虚拟机时却可以 - Why is my output stream seg faulting and my virtual destructor does not work but when i kill virtual it does 为什么这个 C++ 代码不应该打印出变量? - Why does this C++ code print out variables when it is not supposed to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM