This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Function return problem
- To: Steve Grubb <GrubbSR at kscgws00 dot ksc dot nasa dot gov>
- Subject: Re: Function return problem
- From: Alexandre Oliva <oliva at lsd dot ic dot unicamp dot br>
- Date: 20 Sep 1999 12:56:28 -0300
- Cc: gcc at gcc dot gnu dot org
- References: <37E651BE.FC9D66B1@kscgws00.ksc.nasa.gov>
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