Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

后端 未结 9 2312
粉色の甜心
粉色の甜心 2020-11-21 07:37

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?

I understand that you can issue co

相关标签:
9条回答
  • 2020-11-21 07:59

    Under what circumstances doesn't it produce an error? If it declares a return type and doesn't return something, it sounds like an error to me.

    The one exception I can think of is the main() function, which doesn't need a return statement at all (at least in C++; I don't have either of the C standards handy). If there is no return, it will act as if return 0; is the last statement.

    0 讨论(0)
  • 2020-11-21 08:03

    It is a constraint violation in c99, but not in c89. Contrast:

    c89:

    3.6.6.4 The return statement

    Constraints

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

    c99:

    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.

    Even in --std=c99 mode, gcc will only throw a warning (although without needing to enable additional -W flags, as is required by default or in c89/90).

    Edit to add that in c89, "reaching the } that terminates a function is equivalent to executing a return statement without an expression" (3.6.6.4). However, in c99 the behavior is undefined (6.9.1).

    0 讨论(0)
  • 2020-11-21 08:09

    It is legal under C/C++ to not return from a function that claims to return something. There are a number of use cases, such as calling exit(-1), or a function that calls it or throws an exception.

    The compiler is not going to reject legal C++ even if it leads to UB if you are asking it not to. In particular, you are asking for no warnings to be generated. (Gcc still turns on some by default, but when added those seem to align with new features not new warnings for old features)

    Changing the default no-arg gcc to emit some warnings could be a breaking change for existing scripts or make systems. Well designed ones either -Wall and deal with warnings, or toggle individual warnings.

    Learning to use a C++ tool chain is a barrier to learning to be a C++ programmer, but C++ tool chains are typically written by and for experts.

    0 讨论(0)
  • 2020-11-21 08:15

    Sounds like you need to turn up your compiler warnings:

    $ gcc -Wall -Wextra -Werror -x c -
    int main(void) { return; }
    cc1: warnings being treated as errors
    <stdin>: In function ‘main’:
    <stdin>:1: warning: ‘return’ with no value, in function returning non-void
    <stdin>:1: warning: control reaches end of non-void function
    $
    
    0 讨论(0)
  • 2020-11-21 08:22

    You mean, why flowing off the end of a value-returning function (i.e. exiting without an explicit return) is not an error?

    Firstly, in C whether a function returns something meaningful or not is only critical when the executing code actually uses the returned value. Maybe the language didn't want to force you to return anything when you know that you are not going to use it anyway most of the time.

    Secondly, apparently the language specification did not want to force the compiler authors to detect and verify all possible control paths for the presence of an explicit return (although in many cases this is not that difficult to do). Also, some control paths might lead into to non-returning functions - the trait that is generally non known to the compiler. Such paths can become a source of annoying false positives.

    Note also, that C and C++ differ in their definitions of the behavior in this case. In C++ just flowing off the end of a value returning function is always undefined behavior (regardless of whether the function's result is used by the calling code). In C this causes undefined behavior only if the calling code tries to use the returned value.

    0 讨论(0)
  • 2020-11-21 08:23

    gcc does not by default check that all code paths return a value because in general this cannot be done. It assumes you know what you are doing. Consider a common example using enumerations:

    Color getColor(Suit suit) {
        switch (suit) {
            case HEARTS: case DIAMONDS: return RED;
            case SPADES: case CLUBS:    return BLACK;
        }
    
        // Error, no return?
    }
    

    You the programmer know that, barring a bug, this method always returns a color. gcc trusts that you know what you are doing so it doesn't force you to put a return at the bottom of the function.

    javac, on the other hand, tries to verify that all code paths return a value and throws an error if it cannot prove that they all do. This error is mandated by the Java language specification. Note that sometimes it is wrong and you have to put in an unnecessary return statement.

    char getChoice() {
        int ch = read();
    
        if (ch == -1 || ch == 'q') {
            System.exit(0);
        }
        else {
            return (char) ch;
        }
    
        // Cannot reach here, but still an error.
    }
    

    It's a philosophical difference. C and C++ are more permissive and trusting languages than Java or C# and so some errors in the newer languages are warnings in C/C++ and some warnings are ignored or off by default.

    0 讨论(0)
提交回复
热议问题