Re: Representation of _Bool

Liste des GroupesRevenir à cl c 
Sujet : Re: Representation of _Bool
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 19. Jan 2025, 03:28:26
Autres entêtes
Organisation : None to speak of
Message-ID : <87zfjno7ud.fsf@nosuchdomain.example.com>
References : 1 2 3 4
User-Agent : Gnus/5.13 (Gnus v5.13)
learningcpp1@gmail.com (m137) writes:
[...]
It is not documented (see this thread for GCC:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88662). But I think it can
be inferred from the code snippets in Keith's OP and most recent post.
GCC seems to treat all object representations of `_Bool` other than
0b00000000 and 0b00000001 as trap/non-value representations.
I am not sure about Clang, but compiling the last snippet in this
article:
https://www.trust-in-soft.com/resources/blogs/2016-06-16-trap-representations-and-padding-bits with
Clang 19.1.0 and options "-std=c23 -O3 -pedantic" seems to show that
Clang treats `_Bool` as having 254 non-value representations (see here:
https://gcc.godbolt.org/z/4jK9d69P8).

Interesting.

Here's a program based on that snippet:

#include <stdio.h>
#include <string.h>

int f(bool *b) {
    if (*b)
        return 1;
    else
        return 0;
}

int main(void) {
    bool arg;
    unsigned char uc = 123;
    memcpy(&arg, &uc, 1);
    printf("%d\n", f(&arg));
}

With the latest gcc (14.2.0) and clang (19.1.4), it prints 1 when
compiled with "-std=c23 -O0 -pedantic", and 123 when compiled with
-O1, -O2, and -O3.

For gcc, my previous results already indicated that bool has 254
non-value representations.  For clang, the results do seem to
indicate the same thing (though I might argue that it could also
be a bug, unless the clang developers actually intended bool to
have 254 non-value representations).

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
17 Jan 25 * Re: Representation of _Bool15m137
17 Jan 25 +* Re: Representation of _Bool6Kaz Kylheku
17 Jan 25 i+- Re: Representation of _Bool1David Brown
17 Jan 25 i+- Eternal September server retention Was: Representation of _Bool1Michael S
17 Jan 25 i+- Re: Representation of _Bool1James Kuyper
19 Jan 25 i`* Re: Representation of _Bool2m137
19 Jan 25 i `- Re: Representation of _Bool1Keith Thompson
17 Jan 25 +* Re: Representation of _Bool3Tim Rentsch
19 Jan 25 i`* Re: Representation of _Bool2m137
19 Jan 25 i `- Re: Representation of _Bool1Tim Rentsch
17 Jan 25 `* Re: Representation of _Bool5Keith Thompson
18 Jan 25  +- Re: Representation of _Bool1Tim Rentsch
19 Jan 25  `* Re: Representation of _Bool3m137
19 Jan 25   `* Re: Representation of _Bool2Kenny McCormack
21 Jan 25    `- Re: Representation of _Bool1m137

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal