This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Compiler didn't error on function with no return statement
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: "Andy Falanga (afalanga)" <afalanga at micron dot com>
- Cc: David Brown <david dot brown at hesbynett dot no>, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Fri, 21 Jun 2013 15:20:37 +0100
- Subject: Re: Compiler didn't error on function with no return statement
- References: <60F6FAE47D1BCE4380CC06D18F49789B3B3BCB45 at NTXBOIMBX02 dot micron dot com> <51C3765D dot 6010304 at hesbynett dot no> <60F6FAE47D1BCE4380CC06D18F49789B3B3BCF9F at NTXBOIMBX02 dot micron dot com>
On 21 June 2013 15:11, Andy Falanga (afalanga) wrote:
>>
>> <http://stackoverflow.com/questions/5655181/how-to-turn-on-gcc-
>> warnings-for-a-forgotten-return-statement>
>>
>> Failing to provide a return value is legal C, but undefined behaviour.
>> So gcc has to accept it.
>>
>> You only get warnings if you enable them. Normally, you will want your
>> code to be warning-free when compiled with "-Wall". Many people use "-
>> Wextra" as well - I personally enable many of the other warning flags
>> as well.
>>
>
> David,
>
> Thanks for the explanation. I figured it had to be something like this. It's rather humorous that it's legal but undefined. It would seem that anything "legal" must, by requirement, have a definition. One thing I don't quite understand about this behavior of gcc, as I understand the function of gcc it calls the appropriate compiler based on file name suffix. Since this isn't legal C++, why isn't a warning or error generated by the C++ compiler of gcc? Am I misunderstanding something about gcc's operation?
Who said it's not legal C++?
And you *do* get a warning with -Wreturn-type, but you don't get an
error because that would reject legal programs:
void f();
bool g()
{
f();
}
Is this an error? What if f() never returns?
In C++11 we could mark f() with [[noreturn]] (like GCC's
__attribute((noreturn))) but just because a function isn't marked
noreturn doesn't mean it does return.