James Dennett wrote:
> On Thu, Mar 12, 2009 at 4:56 PM, Robert Dewar <[email protected]> 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). Freestanding implementations
may use this to effectively allow a void main().
> More general cases can't be established at compile time, of
> course.
Did you test with -pedantic and/or -std= flags in effect? This usage was
common in K'n'R days, where falling off the end of a function without a return
statement generally returned the result of the most recently-computed
expression. It may be giving only a warning as a gnu extension for back-compat.
cheers,
DaveK