On Thu, Mar 12, 2009 at 11:29 PM, Dave Korn
<dave.korn.cyg...@googlemail.com> wrote:
> James Dennett wrote:
>> On Thu, Mar 12, 2009 at 4:56 PM, Robert Dewar <de...@adacore.com> wrote:
>>> Nathan Ridge wrote:
>>>
>>>> Why does gcc not give an error about this?
>>>> If I compile with "-Wall", it will give a WARNING saying
>>>> "control reaches end of non-void function".
>>>>  However, shouldn't it be an ERROR to return nothing
>>>> from a function that's supposed to return something?
>>>> Does this not result in undefined behaviour? Why goes gcc allow it?
>>> Because the standard does not make this an error, you can't tell
>>> if anyone needs a result, perhaps function is always called in
>>> a void environment.
>>>
>>> A warning is all you can get, always pay attention to warnings!
>>
>> The standard does make it an error, in that if such a function
>> (meaning a function that unconditionally falls off the end, when
>> declared to return a value) is called _at all_ then undefined behavior
>> results.
>
>  No, unless I have missed something..  Not if it is merely called: only if
> the return value is *used* (n1256 #6.9.1.12).

This appears to be a difference between C and C++.  Unfortunate.
6.6.3 [stmt.return]/2 of N2800 says "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."

> Freestanding implementations
> may use this to effectively allow a void main().

There's no need for such trickery; main is special, and has an
implicit "return 0;" (for both C and C++, I believe, since 1999).
IIRC, C allows freestanding implementations to allow "void main" as an
extension (and C++ does not).

-- James

Reply via email to