Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #170687
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Corrupted C99 _Bool? |
| Date | 2023-07-11 19:13 -0700 |
| Organization | None to speak of |
| Message-ID | <87a5w1q3ks.fsf@nosuchdomain.example.com> (permalink) |
| References | <2569e26b-c60a-4873-a759-ee00a50e2bf9n@googlegroups.com> <87edldq4lr.fsf@nosuchdomain.example.com> |
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> "das...@gmail.com" <dashley@gmail.com> writes:
>> C99 indicates that a _Bool is an unsigned integer type. When a
>> variable of a type other than _Bool is assigned to a _Bool, C99
>> specifies that the _Bool receives the value of 0 if the variable was
>> 0, or 1 otherwise.
>>
>> But what does C99 say about a _Bool that is corrupted to a value other
>> than 0 or 1 (perhaps through a pointer error in another part of the
>> program)? Will this corrupted _Bool still function correctly?
>>
>> For example:
>>
>> _Bool a = true;
>> _Bool b; /* Assume b is corrupted to "2" somehow */
>>
>> if (a && b)
>> {
>> /* Would the compiler be able to use a bitwise */
>> /* AND above, because it assumes all _Bools */
>> /* must be 0 or 1, so that if (1 && 2) becomes */
>> /* if (1 & 2) and evaluates false? */
>> }
>>
>> Thanks for any clarification.
>
> I'm reasonably sure that anything that would cause a _Bool object to
> have a value other than 0 or 1, or at least anything that tried to
> access such an object's value, would have undefined behavior.
>
> Using bitwise AND for the code above is one of the infinitely many
> things a compiler could do.
And C23 (see the N3096 public draft) clarifies that bool has one value
bit (sizeof(bool)*CHAR_BIT)-1 padding bits. (bool is a keyword in C23;
_Bool and <stdbool.h> are kept for backward compatibility.)
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Corrupted C99 _Bool? "das...@gmail.com" <dashley@gmail.com> - 2023-07-11 17:32 -0700
Re: Corrupted C99 _Bool? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-11 18:51 -0700
Re: Corrupted C99 _Bool? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-11 19:13 -0700
Re: Corrupted C99 _Bool? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-12 02:47 -0400
Re: Corrupted C99 _Bool? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-20 21:25 -0700
Re: Corrupted C99 _Bool? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-21 01:58 -0700
Re: Corrupted C99 _Bool? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-21 14:22 -0700
Re: Corrupted C99 _Bool? David Brown <david.brown@hesbynett.no> - 2023-07-12 09:31 +0200
Re: Corrupted C99 _Bool? Opus <ifonly@youknew.org> - 2023-07-12 22:10 +0200
Re: Corrupted C99 _Bool? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-12 14:21 -0700
Re: Corrupted C99 _Bool? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-20 22:17 -0700
Re: Corrupted C99 _Bool? James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-07-12 18:44 -0400
Re: Corrupted C99 _Bool? "das...@gmail.com" <dashley@gmail.com> - 2023-07-12 16:41 -0700
Re: Corrupted C99 _Bool? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-07-13 01:17 +0100
Re: Corrupted C99 _Bool? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-12 17:28 -0700
Re: Corrupted C99 _Bool? David Brown <david.brown@hesbynett.no> - 2023-07-13 11:27 +0200
Re: Corrupted C99 _Bool? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2023-07-13 15:18 -0700
Re: Corrupted C99 _Bool? David Brown <david.brown@hesbynett.no> - 2023-07-14 09:07 +0200
Re: Corrupted C99 _Bool? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2023-07-14 00:53 -0700
Re: Corrupted C99 _Bool? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-07-20 22:09 -0700
csiph-web