This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Function return problem


On Sep 20, 1999, Steve Grubb <GrubbSR@kscgws00.ksc.nasa.gov> wrote:

> int badFunction(void)
> {
>  if (++count%2)
>   return rand();
>     // problem is here...no return for implied else
> }

> A survey of many compilers shows they accept this code and some
> compilers (gcc) generate warnings. The question, why would this not
> be an error?

Because it is not ill-formed.  Falling off the end of a non-void
function is undefined behavior, but, if the compiler can't prove that
the undefined behavior *will* be exercised, it mustn't reject the
program.  For example, if this function is never called, it can't
reject the code.  If it is only called when count is even, it will
always return a valid value.

-- 
Alexandre Oliva http://www.dcc.unicamp.br/~oliva IC-Unicamp, Bra[sz]il
oliva@{lsd.ic.unicamp.br,guarana.{org,com}} aoliva@{acm,computer}.org
oliva@{gnu.org,kaffe.org,{egcs,sourceware}.cygnus.com,samba.org}
** I may forward mail about projects to mailing lists; please use them


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]