非空函数中的空 return 是未定义的行为吗?
Empty return in non-void function, is undefined behaviour?
阅读有关 control reaches end of non-void functions 主题的答案后,我没有看到任何答案专门针对退出非 void 函数的情况一个空的 return
语句:
int return_integer() { return; } // empty return in non-void function
到目前为止我在 C standard 中发现的是:
6.8.6.4 The return statement
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
.
标准引述说明应该我们对void
和非void
函数的return
语句做什么,当我们忽略了文档其他部分提到的约束:
6.9.1 Function definitions
- If the
}
that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
之前的标准引用指出,如果我们使用函数中的 returned 值,该函数在到达右大括号 (}
) 后结束,就会发生 UB,因此我们在下面的代码中有 UB :
int UB(int x) { if (x) return x; }
printf("%d", UB(1)); // Correct
printf("%d", UB(0)); // Undefined behavior
在UB(1)
中通过if (x)
下的return x;
指令调用函数returns 1
;在 UB(0)
调用中,if (x)
条件未通过,因此函数结束达到 }
,在这种情况下使用 return 值是 UB(但不在 UB(1)
).但是,在这种情况下会发生什么?
int UB(int x) { if (x) return; } // empty return statement
printf("%d", UB(1)); // Undefined behavior?
printf("%d", UB(0)); // Undefined behavior
在上面的代码中,调用UB(1)
不满足导致UB的§6.9.1/12
要求,因为函数结束没有到达}
也没有 return 任何值。
C标准的哪一部分描述了这种情况?
int UB(int x) { if (x) return; }
这甚至不是未定义的行为,它是约束违规。引用的文字
A return statement without an expression shall only appear in a
function whose return type is void
从 6.8.6.4 开始是规范的,这意味着编译器不允许在不给出诊断消息的情况下让它溜走。如果它在没有给出诊断的情况下编译,则编译器不是一个符合标准的实现(不遵循语言标准)。
用简单的英语来说,这意味着:该代码甚至不应该编译。
现在,如果编译器确实生成了二进制可执行文件,即使代码违反了约束条件,那么所有的赌注都没有了。它不再是 C 程序,而是一些非标准程序,任何语言标准都不能保证其行为。
阅读有关 control reaches end of non-void functions 主题的答案后,我没有看到任何答案专门针对退出非 void 函数的情况一个空的 return
语句:
int return_integer() { return; } // empty return in non-void function
到目前为止我在 C standard 中发现的是:
6.8.6.4 The return statement
Constraints
- A
return
statement with an expression shall not appear in a function whose return type isvoid
. Areturn
statement without an expression shall only appear in a function whose return type isvoid
.
标准引述说明应该我们对void
和非void
函数的return
语句做什么,当我们忽略了文档其他部分提到的约束:
6.9.1 Function definitions
- If the
}
that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
之前的标准引用指出,如果我们使用函数中的 returned 值,该函数在到达右大括号 (}
) 后结束,就会发生 UB,因此我们在下面的代码中有 UB :
int UB(int x) { if (x) return x; }
printf("%d", UB(1)); // Correct
printf("%d", UB(0)); // Undefined behavior
在UB(1)
中通过if (x)
下的return x;
指令调用函数returns 1
;在 UB(0)
调用中,if (x)
条件未通过,因此函数结束达到 }
,在这种情况下使用 return 值是 UB(但不在 UB(1)
).但是,在这种情况下会发生什么?
int UB(int x) { if (x) return; } // empty return statement
printf("%d", UB(1)); // Undefined behavior?
printf("%d", UB(0)); // Undefined behavior
在上面的代码中,调用UB(1)
不满足导致UB的§6.9.1/12
要求,因为函数结束没有到达}
也没有 return 任何值。
C标准的哪一部分描述了这种情况?
int UB(int x) { if (x) return; }
这甚至不是未定义的行为,它是约束违规。引用的文字
A return statement without an expression shall only appear in a function whose return type is void
从 6.8.6.4 开始是规范的,这意味着编译器不允许在不给出诊断消息的情况下让它溜走。如果它在没有给出诊断的情况下编译,则编译器不是一个符合标准的实现(不遵循语言标准)。
用简单的英语来说,这意味着:该代码甚至不应该编译。
现在,如果编译器确实生成了二进制可执行文件,即使代码违反了约束条件,那么所有的赌注都没有了。它不再是 C 程序,而是一些非标准程序,任何语言标准都不能保证其行为。