空值返回函数g可以返回f();当f返回void?

时间:2022-03-18 20:03:56

Consider the following snippet:

请考虑以下代码段:

void f(void);

void g(…)
{
  …
  return f();
  …
}

Is this return f(); valid according to C11?

这是返回f();根据C11有效吗?

I am not advocating using this pattern: if it works at all, it is obviously equivalent to f(); return; (where the return; itself would be redundant if this is at the end of function g()). I am asking this question in the context of the static analysis of C programs, where the C code has already been written by someone else and the question is deciding whether or not it is valid according to the standard.

我并不主张使用这种模式:如果它起作用,它显然等同于f();返回; (返回;如果它在函数g()的末尾,则本身将是多余的)。我在C程序的静态分析的上下文中问这个问题,其中C代码已经由其他人编写,问题是根据标准判断它是否有效。

I would interpret C11 6.8.6.4:1 as meaning that it is non-standard and should be statically rejected. Is it possible to interpret it differently (I have found this pattern in actual and otherwise high-quality source code)?

我认为C11 6.8.6.4:1意味着它是非标准的并且应该被静态拒绝。是否有可能以不同的方式解释它(我在实际和其他高质量的源代码中找到了这种模式)?

Constraints

约束

A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

带有表达式的return语句不应出现在返回类型为void的函数中。不带表达式的return语句只能出现在返回类型为void的函数中。

3 个解决方案

#1


18  

Anything after return is an expression.

返回后的任何东西都是表达式。

6.8.6:1 Jump statements

Syntax  

   ...
   return expressionopt; 

And standard says that:

标准说:

A return statement with an expression shall not appear in a function whose return type is void. ....

带有表达式的return语句不应出现在返回类型为void的函数中。 ....

f() is also an expression here. The compiler should raise a warning

f()也是这里的一个表达式。编译器应该发出警告

[Warning] ISO C forbids 'return' with expression, in function returning void [-pedantic]

#2


10  

This clearly is a constraint violation, in particular in view of

这显然是违反约束的行为,特别是考虑到这一点

6.3.2.2 void: The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way,

6.3.2.2 void:void表达式(具有void类型的表达式)的(不存在)值不得以任何方式使用,

That means that the incomplete type void is a dead end that cannot be reused for any purpose whatsoever.

这意味着不完整类型的空洞是一个死胡同,不能为任何目的重复使用。

#3


0  

It clearly states A return statement without an expression shall only appear in a function whose return type is void, try and execute this:

它清楚地说明没有表达式的return语句只出现在返回类型为void的函数中,请尝试执行:

void g()
{
    return; // does not return any expression at all
}
void f()
{
    return g();
}


int main(void) {
    f();
    return 0;
}

#1


18  

Anything after return is an expression.

返回后的任何东西都是表达式。

6.8.6:1 Jump statements

Syntax  

   ...
   return expressionopt; 

And standard says that:

标准说:

A return statement with an expression shall not appear in a function whose return type is void. ....

带有表达式的return语句不应出现在返回类型为void的函数中。 ....

f() is also an expression here. The compiler should raise a warning

f()也是这里的一个表达式。编译器应该发出警告

[Warning] ISO C forbids 'return' with expression, in function returning void [-pedantic]

#2


10  

This clearly is a constraint violation, in particular in view of

这显然是违反约束的行为,特别是考虑到这一点

6.3.2.2 void: The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way,

6.3.2.2 void:void表达式(具有void类型的表达式)的(不存在)值不得以任何方式使用,

That means that the incomplete type void is a dead end that cannot be reused for any purpose whatsoever.

这意味着不完整类型的空洞是一个死胡同,不能为任何目的重复使用。

#3


0  

It clearly states A return statement without an expression shall only appear in a function whose return type is void, try and execute this:

它清楚地说明没有表达式的return语句只出现在返回类型为void的函数中,请尝试执行:

void g()
{
    return; // does not return any expression at all
}
void f()
{
    return g();
}


int main(void) {
    f();
    return 0;
}

相关文章