How to handle error: expected expression before ‘do’ when there is no "do"?
How to handle error: expected expression before ‘do’ when there is no "do"?
我收到以下编译器错误,即使我的代码中没有 "do" 表达式。
gcc -Wall -g -c main.c -lasound
In file included from /usr/include/alsa/asoundlib.h:49:0,
from main.c:2:
main.c: In function ‘main’:
main.c:8:5: error: expected expression before ‘do’
if(snd_pcm_hw_params_alloca(¶ms) < 0) {
^
main.c:6:30: warning: unused variable ‘params’ [-Wunused-variable]
snd_pcm_hw_params_t *params;
^~~~~~
Makefile:15: recipe for target 'main.o' failed
make: *** [main.o] Error 1
来自以下最小可重现示例:
#include <stdio.h>
#include <alsa/asoundlib.h>
int main(int argc, char **argv)
{
snd_pcm_hw_params_t *params;
if(snd_pcm_hw_params_alloca(¶ms) < 0) {
return 0;
}
exit(0);
}
我知道这不是有效的 ALSA 程序。我也知道 snd_pcm_hw_params_alloca() 似乎甚至 return 没有任何值得检查错误的东西?但这无关紧要,无论如何,这应该是有效的 C 代码,即使它滥用了 API.
"do" 表达式在哪里?如果我去 /usr/include/alsa/asoundlib.h 并在那里四处寻找,我看不到任何明显的问题。
如果我删除条件 if 测试,得到:
#include <stdio.h>
#include <alsa/asoundlib.h>
int main(int argc, char **argv)
{
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
exit(0);
}
这将编译没有错误。
这是什么?
如果我查看 pcm.h,我会看到:
#define snd_pcm_hw_params_alloca(ptr) __snd_alloca(ptr, snd_pcm_hw_params)
int snd_pcm_hw_params_malloc(snd_pcm_hw_params_t **ptr);
void snd_pcm_hw_params_free(snd_pcm_hw_params_t *obj);
void snd_pcm_hw_params_copy(snd_pcm_hw_params_t *dst, const snd_pcm_hw_params_t *src);
但是,这并没有告诉我任何信息。为什么编译器会产生这个错误?
I'm also aware that it appears snd_pcm_hw_params_alloca() doesn't even return anything worthwhile to check for errors against? That's not relevant though, this should valid C code regardless, even if it abuses the API.
不,如果 snd_pcm_hw_params_alloca()
没有值,您不能将它与 0 进行比较。例如,以下内容也是无效的:
void func(void) { }
void other(void) {
if (func() < 0) { // Error
}
}
实际上,snd_pcm_hw_params_alloca()
是一个宏,它是另一个宏__snd_alloca
的包装器。 do
是为了让它表现得更像一个语句。您只能将其称为单独一行的语句,或其他任何 do
循环合法的地方。
snd_pcm_hw_params_alloca(¶ms);
您无法检查错误,因为 alloca()
不检查错误。如果 alloca()
失败,它只会踩到你的堆栈上,坏事就会发生。你对此无能为力,除了不使用 alloca()
(这就是为什么你可能会听到避免使用 alloca 的建议)。
有关为什么使用 do
循环的解释,请参阅:C multi-line macro: do/while(0) vs scope block
有关 alloca()
工作原理的更多信息,请参阅:Why is the use of alloca() not considered good practice?
我收到以下编译器错误,即使我的代码中没有 "do" 表达式。
gcc -Wall -g -c main.c -lasound
In file included from /usr/include/alsa/asoundlib.h:49:0,
from main.c:2:
main.c: In function ‘main’:
main.c:8:5: error: expected expression before ‘do’
if(snd_pcm_hw_params_alloca(¶ms) < 0) {
^
main.c:6:30: warning: unused variable ‘params’ [-Wunused-variable]
snd_pcm_hw_params_t *params;
^~~~~~
Makefile:15: recipe for target 'main.o' failed
make: *** [main.o] Error 1
来自以下最小可重现示例:
#include <stdio.h>
#include <alsa/asoundlib.h>
int main(int argc, char **argv)
{
snd_pcm_hw_params_t *params;
if(snd_pcm_hw_params_alloca(¶ms) < 0) {
return 0;
}
exit(0);
}
我知道这不是有效的 ALSA 程序。我也知道 snd_pcm_hw_params_alloca() 似乎甚至 return 没有任何值得检查错误的东西?但这无关紧要,无论如何,这应该是有效的 C 代码,即使它滥用了 API.
"do" 表达式在哪里?如果我去 /usr/include/alsa/asoundlib.h 并在那里四处寻找,我看不到任何明显的问题。
如果我删除条件 if 测试,得到:
#include <stdio.h>
#include <alsa/asoundlib.h>
int main(int argc, char **argv)
{
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
exit(0);
}
这将编译没有错误。
这是什么?
如果我查看 pcm.h,我会看到:
#define snd_pcm_hw_params_alloca(ptr) __snd_alloca(ptr, snd_pcm_hw_params)
int snd_pcm_hw_params_malloc(snd_pcm_hw_params_t **ptr);
void snd_pcm_hw_params_free(snd_pcm_hw_params_t *obj);
void snd_pcm_hw_params_copy(snd_pcm_hw_params_t *dst, const snd_pcm_hw_params_t *src);
但是,这并没有告诉我任何信息。为什么编译器会产生这个错误?
I'm also aware that it appears snd_pcm_hw_params_alloca() doesn't even return anything worthwhile to check for errors against? That's not relevant though, this should valid C code regardless, even if it abuses the API.
不,如果 snd_pcm_hw_params_alloca()
没有值,您不能将它与 0 进行比较。例如,以下内容也是无效的:
void func(void) { }
void other(void) {
if (func() < 0) { // Error
}
}
实际上,snd_pcm_hw_params_alloca()
是一个宏,它是另一个宏__snd_alloca
的包装器。 do
是为了让它表现得更像一个语句。您只能将其称为单独一行的语句,或其他任何 do
循环合法的地方。
snd_pcm_hw_params_alloca(¶ms);
您无法检查错误,因为 alloca()
不检查错误。如果 alloca()
失败,它只会踩到你的堆栈上,坏事就会发生。你对此无能为力,除了不使用 alloca()
(这就是为什么你可能会听到避免使用 alloca 的建议)。
有关为什么使用 do
循环的解释,请参阅:C multi-line macro: do/while(0) vs scope block
有关 alloca()
工作原理的更多信息,请参阅:Why is the use of alloca() not considered good practice?