gpt4 book ai didi

c++ - 为什么我的返回元组中只有一个有效,但打印出来时却没问题?

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:02 25 4
gpt4 key购买 nike

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

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

给出这个程序的背景。我正在读取一个 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,因此它的采样超出了我的数组大小。调试功能必须在数组和其他值之间添加一些缓冲,以使其能够正确输出。

关于c++ - 为什么我的返回元组中只有一个有效,但打印出来时却没问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com