简体   繁体   中英

Why does this compile on Ideone?

Ok so I was messing around on Ideone and accidentally submitted this piece of code, however to my surprise it actually compiled and ran outputting a value of 0, here .

#include <iostream>

using namespace std;

const int five(  )
{
        const int i = 5;
}

int main() {
        cout << five(  ) << endl;
        return 0;
}

I then tried this in Visual Studio, and on Codepad however both failed to compile because five() does not return a value, as one would expect. My question, is of course, why does this compile fine on Ideone even though the code, to my understanding is wrong and shouldn't compile.

Plain and simply (from C++11 6.6.3 "The return statement"):

Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.

So the compiler is pretty much allowed to do whatever it wants. Clearly, a diagnostic is something I'd prefer from a compiler, but there are times when it can be difficult to diagnose (like when the return is inside conditional logic, and the 'end' of the function would never be reached).

Note that I get the following warning with GCC 4.6.1 (using the Wall option):

test.cpp:8:1: warning: no return statement in function returning non-void [-Wreturn-type]

I'm not sure what options ideone passes to GCC (I imagine that -Wall would do the same with the 4.3.4 version that ideone uses).

Some related information:

In C it's OK for a function that is declared to return a value to not actually do so in certain circumstances; in C it only results in undefined behavior if the function's return value is actually used . Pre-standard C didn't always support the void type, so functions that didn't return anything were often declared to return int , explicitly or implicitly. From C99 6.9.1/12 "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.

Also, as mentioned in a couple comments, flowing off the end of main() is treated specially by C++ and C99 and later.

Not returning a value from a non-void function is a mistake, but not all compiler treats it as an error -- for example, GCC only emits a warning when it encounters this. Other compilers may be paranoid (and they're right) and don't let you compile such code. Of course compiler behavior may be modified using different switches and options.

The return value of 0 is just a random value -- it might equally be 255, -1 or any other garbage, as doing this is undefined behavior (except for main, for which C99 specifies that an implicit 0 return value should be assumed).

It appears that ideone doesn't display warnings, it only displays the compiler output if there was an error. On the version of GCC that ideone is using (gcc 4.3) this isn't an error, it's just a warning.

The code has undefined behavior. Even though what you're doing is wrong, the compiler is not required to diagnose it. The other point is that ideone uses what is now a fairly old version of gcc. A reasonably current version of gcc (eg, 4.7) will at least give you a warning that your function is declared to return a value, but doesn't -- but not by default. You have to turn it on with something like -Wall to get the warning (but as a general rule, I'd always use at least -Wall with gcc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM