[英]C Function: return value incorrect?
I have written a C library function, but the return value seems to be incorrect, even though it is correct in the function. 我编写了一个C库函数,但返回值似乎不正确,即使它在函数中是正确的。
Here is relevant code: 这是相关代码:
(In dcml_private.c) The offending function: (在dcml_private.c中)违规函数:
dcml_status _dcml_get_status(struct dcml_device *dev)
{
uint64_t data;
dcml_status ret;
int len;
libusb_bulk_transfer(dev->handle,
DCML_ENDPOINT | LIBUSB_ENDPOINT_IN,
(unsigned char *) &data,
DCML_REPORT_SZ, &len, RX_TIMEOUT);
printf("data = %ld\n", data);
if (len != DCML_REPORT_SZ)
printf("DCML: LIBUSB ERROR (%s)\n", libusb_error_name(len));
return STATUS_UNKNOWN;
ret = data & ~(1>>17);
return (ret);
}
The calling function: 调用功能:
void _dcml_cmd(dcml_context *ctx, dcml_cmd cmd,
dcml_status quit_cond, int dur)
{
struct timeval start;
struct timeval cur;
uint32_t stat;
(void)gettimeofday(&start, NULL);
(void)gettimeofday(&cur, NULL);
_dcml_send_cmd(ctx->active, cmd);
while(difftimeval(cur, start) < dur) {
sleep(POLL_PERIOD);
stat = _dcml_get_status(ctx->active);
printf("status (%d), quit_cond (%d)", stat, quit_cond);
if (stat == quit_cond)
break;
(void)gettimeofday(&cur, NULL);
}
_dcml_send_cmd(ctx->active, CMD_NONE);
}
As you see, I have print statements in my functions. 如你所见,我的函数中有print语句。 In _dcml_cmd, typical output of that print statement would be
在_dcml_cmd中,该print语句的典型输出将是
status (65535), quit_cond (2048)
Where _dcml_get_status prints: _dcml_get_status打印的位置:
data = 128
What this means is that the return value is correct IMMEDIATELY before exiting _dcml_get_status, but incorrect immediately after it is return to the calling function (and always has a value of 65535 here...) 这意味着返回值在退出_dcml_get_status之前立即正确,但在返回调用函数后立即错误(此处的值总是为65535 ......)
It's probably helpful to know that "dmcl_status" is an enum. 知道“dmcl_status”是一个枚举可能会有所帮助。 Switching the return type to uint16_t does not fix the issue.
将返回类型切换为uint16_t无法解决问题。 I thought it might be an overflow issue or something, but changing types, explicit casts and adding a mask line doesn't fix it.
我认为它可能是一个溢出问题或者其他什么,但改变类型,显式转换和添加掩码行并不能解决它。
Any thoughts? 有什么想法吗?
It's because you have a bad habit of not putting { } after your if statements ALWAYS 这是因为你有一个坏习惯,就是不要在你的if语句之后放{}
if (len != DCML_REPORT_SZ) {
printf("DCML: LIBUSB ERROR (%s)\n", libusb_error_name(len));
return STATUS_UNKNOWN;
}
also use fprintf(stderr,...
to dump out errors. Sending errors to printf is bad practice: 还使用
fprintf(stderr,...
来转储错误。向printf发送错误是不好的做法:
if (len != DCML_REPORT_SZ) {
fprintf(stderr, "DCML: LIBUSB ERROR (%s)\n", libusb_error_name(len));
return STATUS_UNKNOWN;
}
The calling function has the same issues around: 调用函数有相同的问题:
printf("status (%d), quit_cond (%d)", stat, quit_cond);
if (stat == quit_cond)
break;
Use: 采用:
fprintf(stderr, "status (%d), quit_cond (%d)", stat, quit_cond);
if (stat == quit_cond) {
break;
}
Yes it uses up an extra line but darn it at 3:00AM when you are debugging this by adding fprintfs all over the place it won't break your logic. 是的,它会占用一条额外的线,但是在凌晨3点进行调试时,通过在整个地方添加fprintf来调试它,它不会破坏你的逻辑。 :^)
:^)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.