我的部分代码有问题,经过一些迭代后,它似乎将 NaN 读取为double
结构的 a 值。我想我找到了错误,但我仍然想知道为什么 gcc(带有busybox 的嵌入式 Linux 上的版本 3.2.3)没有警告我。以下是代码的重要部分:
用于通过 USB 获取数据的函数的 c 文件及其头文件:
// usb_control.h
typedef struct{
double mean;
short *values;
} DATA_POINTS;
typedef struct{
int size;
DATA_POINTS *channel1;
//....7 more channels
} DATA_STRUCT;
DATA_STRUCT *create_data_struct(int N); // N values per channel
int free_data_struct(DATA_STRUCT *data);
int aqcu_data(DATA_STRUCT *data, int N);
带有辅助函数(数学、位移等...)的 c 和头文件:
// helper.h
int mean(DATA_STRUCT *data);
// helper.c (this is where the error is obviously)
double mean(DATA_STRUCT *data)
{
// sum in for loop
data->channel1->mean = sum/data->N;
// ...7 more channels
// a printf here displayed the mean values corretly
}
主文件
// main.c
#include "helper.h"
#include "usb_control.h"
// Allocate space for data struct
DATA_STRUCT *data = create_data_struct(N);
// get data for different delays
for (delay = 0; delay < 500; delay += pw){
acqu_data(data, N);
mean(data);
printf("%.2f",data->channel1->mean); // done for all 8 channels
// printf of the mean values first is correct. Than after 5 iterations
// it is always NaN for channel1. The other channels are displayed correctly;
}
没有段错误,也没有任何其他错误行为,只有主文件中 channel1 的 NaN。
发现错误后,这并不容易,当然是东修复。定义中的返回类型mean(){}
错误。而不是double mean()
它必须int mean()
像原型定义的那样。当所有函数都放在一个文件中时,gcc 会警告我有一个重新定义的函数mean()
。但是当我分别编译每个 c 文件并在之后链接它们时,gcc 似乎错过了这一点。
所以我的问题是。为什么我没有收到任何警告,即使没有 gcc -Wall?或者是否还有另一个隐藏的错误现在不会引起问题?
问候,基督徒