http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46899

--- Comment #7 from Eskil Steenberg <eskil at obsession dot se> 2010-12-12 
21:46:18 UTC ---
Hi

> No, it is undefined at runtime.  This again is not an undefined behavior
> at
> compile time but rather at runtime.  What that means is the behavior
> cannot be
> diagnosed (at least by the C standard definitions) at compile time and has
> to
> compile.  There is no undefined at compile time behavior here, only at
> runtime.

Well the compiler does make assumptions about what the runtime will do.
Have a look at this function:

void my_func(unsigned short a, unsigned short b)
{
    unsigned int c;
    c = a * b;

    printf("c = %u\n", c);
    if(f < 3000000000) /* 3 billion */
        printf("foo bar\n");
}

using gcc you can get the output (with the input ffff, and ffff):

c = 4294836225
foo bar

This output should not be possible and will be very confusing to the user
(it was to me). My (limited) reading of the C spec should not support this
even though i understand why it happens.

At compile time the compiler decides that c can never be larger then max
singned int, and therefor it thinks that it can optimize away the if
statement.

Cheers

E

PS sorry I dont have a compiler on this machine so excuse any typos.

Reply via email to