Hi,

why does gcc (4.4.7 and 4.8.2) sometimes warn and sometimes not warn
when undefined behavior is invoked when making illegal function
pointer conversions?

For instance, consider the code below:

---------
/* Tested with gcc 4.4.7 and 4.8.2 */
#include <stdio.h>
#include <stdbool.h>

bool boolFunctionThatReturnsFalse() { return false; }

typedef int (*intFunction)(void);

int
main()
{
    /* no warning here, just undefined behavior */
    intFunction f = (intFunction) boolFunctionThatReturnsFalse;
    if ( f() )
        printf("true\n");
    else
        printf("false\n");

    /* gcc warns and emits abort code */
    if ( ((intFunction) boolFunctionThatReturnsFalse) () )
        printf("true\n");
    else
        printf("false\n");
}

---------------

Why does assigning boolFunctionThatReturnsFalse to a variable f after
the cast, instead of directly dereferencing it, silence the compiler's
warnings?

Thanks.

 - Godmar

ps: I would like to see the warning, of course, since casting a bool
returning function to an int returning function is undefined behavior.

Reply via email to