为什么在不返回值的情况下从非空函数的末尾流出不会产生编译器错误? [英] Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

查看:25
本文介绍了为什么在不返回值的情况下从非空函数的末尾流出不会产生编译器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我多年前意识到默认情况下这不会产生错误(至少在 GCC 中),我一直想知道为什么?

Ever since I realized many years ago, that this doesn't produce an error by default (in GCC at least), I've always wondered why?

我知道你可以发出编译器标志来产生警告,但它不应该总是一个错误吗?为什么非 void 函数不返回有效值是有意义的?

I understand that you can issue compiler flags to produce a warning, but shouldn't it always be an error? Why does it make sense for a non-void function not returning a value to be valid?

评论中要求的示例:

#include <stdio.h>
int stringSize()
{
}

int main()
{
    char cstring[5];
    printf( "the last char is: %c
", cstring[stringSize()-1] ); 
    return 0;
}

...编译.

推荐答案

C99 和 C++ 标准不要求函数返回值.返回值函数中缺少的 return 语句将仅在 main 函数中定义(返回 0).

C99 and C++ standards don't require functions to return a value. The missing return statement in a value-returning function will be defined (to return 0) only in the main function.

基本原理包括检查每个代码路径是否都返回一个值非常困难,并且可以使用嵌入式汇编程序或其他棘手的方法设置返回值.

The rationale includes that checking if every code path returns a value is quite difficult, and a return value could be set with embedded assembler or other tricky methods.

来自 C++11 草案:

§6.6.3/2

从函数的末尾流出 [...] 会导致返回值的函数中出现未定义的行为.

Flowing off the end of a function [...] results in undefined behavior in a value-returning function.

§ 3.6.1/5

如果控制到达main的末尾,没有遇到return语句,效果就是执行

If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;

请注意,C++ 6.6.3/2 中描述的行为与 C 中的不同.

Note that the behaviour described in C++ 6.6.3/2 is not the same in C.

如果你使用 -Wreturn-type 选项调用 gcc 会给出警告.

gcc will give you a warning if you call it with -Wreturn-type option.

-Wreturn-type 每当函数定义的返回类型为默认为整数.还警告任何没有返回值的 return 语句在返回类型不是的函数中void(从结尾处掉下来)函数体被认为是返回没有价值),以及关于回报带有表达式的语句返回类型为 void 的函数.

-Wreturn-type Warn whenever a function is defined with a return-type that defaults to int. Also warn about any return statement with no return-value in a function whose return-type is not void (falling off the end of the function body is considered returning without a value), and about a return statement with an expression in a function whose return-type is void.

此警告由 -Wall 启用.

<小时>

出于好奇,看看这段代码做了什么:


Just as a curiosity, look what this code does:

#include <iostream>

int foo() {
   int a = 5;
   int b = a + 1;
}

int main() { std::cout << foo() << std::endl; } // may print 6

此代码具有正式未定义的行为,实际上它是调用约定架构 依赖.在一个特定的系统上,使用一个特定的编译器,返回值是最后一个表达式评估的结果,存储在该系统处理器的 eax 寄存器中.

This code has formally undefined behaviour, and in practice it's calling convention and architecture dependent. On one particular system, with one particular compiler, the return value is the result of last expression evaluation, stored in the eax register of that system's processor.

这篇关于为什么在不返回值的情况下从非空函数的末尾流出不会产生编译器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆