Sujet : Re: Representation of _Bool
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 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.comvoid Void(void) { Void(); } /* The recursive call of the void */