如何处理错误:在没有“做”的情况下,在“做”之前的期望表达?

问题描述 投票:0回答:1

即使我的代码中没有“ 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(&params) < 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(&params) < 0) {
                return 0;
        }

        exit(0);
}

我知道这不是有效的ALSA程序。我也知道,看来snd_pcm_hw_params_alloca()甚至没有返回值得检查错误的东西?但这无关紧要,即使滥用了API,这也应该有效的C代码。

“做”在哪里?如果我转到/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(&params);

        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);

但是,这什么也没告诉我。为什么编译器会产生此错误?

c compiler-errors alsa
1个回答
0
投票

我也知道snd_pcm_hw_params_alloca()似乎还没有返回值得检查错误的内容吗?但这无关紧要,即使滥用了API,这也应该有效的C代码。

否,如果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(&params);

您无法检查错误,因为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?

© www.soinside.com 2019 - 2024. All rights reserved.