[Bug c/67433] ?: expression returns unexpected value when condition is a bool variable which memory is not true/false

xuejuncao at gmail dot com gcc-bugzilla@gcc.gnu.org
Wed Sep 2 07:53:00 GMT 2015


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67433

xuejuncao <xuejuncao at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|?: expresion returns        |?: expression returns
                   |unexpected value when       |unexpected value when
                   |condition is a bool         |condition is a bool
                   |variable and it's value     |variable which memory is
                   |above 1                     |not true/false

--- Comment #1 from xuejuncao <xuejuncao at gmail dot com> ---
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int main()
{
    union u {
        bool b;
        char c;
    } u;
    memset((void *)&u, -1, sizeof(u));
    bool b = (u.b ? 1 : 0);
    int i = (u.b ? 1 : 0);
    int j = (u.b ? 1 : 2);
    printf("b = %d, i = %d, j = %d\n", b, i, j);
    return 0;
}

without optimisation, the output is:
b = 1, i = 255, j = 1

with -O2, is:
b = 255, i = 255, j = 1



More information about the Gcc-bugs mailing list