Deutsch   English   Français   Italiano  
<v95fcj$pv2g$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: how cast works?
Date: Fri, 9 Aug 2024 18:16:19 +0200
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <v95fcj$pv2g$1@dont-email.me>
References: <v8vlo9$2oc1v$1@dont-email.me> <slrnvb7kis.28a.dan@djph.net>
 <v929ah$3u7l7$1@dont-email.me> <v92gt1$e1l$1@dont-email.me>
 <20240808193203.00006287@yahoo.com> <v92va5$4msg$1@dont-email.me>
 <v9310a$4v1a$2@dont-email.me> <v93565$6ffo$1@dont-email.me>
 <v93h12$9vom$1@dont-email.me> <87frre8v5q.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 09 Aug 2024 18:16:20 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="20dc90042b48f6417a3734e9d0abbff5";
	logging-data="851024"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+cyBIqTIFzEaYOpWuCMieVIX3C1zP7+jU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:OlQITrA/iNCCjIez7I/d71FNuTc=
In-Reply-To: <87frre8v5q.fsf@nosuchdomain.example.com>
Content-Language: en-GB, nb-NO
Bytes: 3352

On 09/08/2024 01:14, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
> [...]
>> A _Bool is always either 0 or 1.  The conversion is whatever the
>> compiler needs to give an int of value 0 or 1.
> 
> The value of a _Bool object is always either 0 or 1 *unless* the program
> does something weird.

True.  But attempting to use a _Bool object (as a _Bool) that does not 
contain either 0 or 1 is going to be undefined behaviour (at least it 
was on the platform where I saw this happen as a code bug).

> 
> C23 is a bit clearer about the representation of bool (still also called
> _Bool) than previous editions.  It states (draft N3220) that :
> 
>      The type bool shall have one value bit and (sizeof(bool)*CHAR_BIT)-1
>      padding bits.
> 
> There are several ways to force a representation other than 00000000 or
> 00000001 into a _Bool object, including a union, memset(), or type
> punning via a pointer cast.
> 
> C23 dropped the term "trap representation", replacing it with "non-value
> representation" -- a reasonable change, since accessing a trap
> representation is not guaranteed to cause the program to "perform a
> trap" (defined as "interrupt execution of the program such that no
> further operations are performed").
> 
> It doesn't specify whether setting the padding bits to 1 results in a
> non-value representation.

That's probably an implementation-defined issue, is it not?

> 
> If non-zero padding bits create a non-value representation, then
> accessing a bool object holding such a representation has undefined
> behavior.  It could, among other things, yield the same value implied by
> the representation as if it were an ordinary integer of the same size.
> 
> If there are no non-value representations, then only the
> single value bit determines the value, which is either false or true.
> 
> As you mentioned, I expect that sizeof (bool) will normally be 1, but an
> implementation could make it wider, e.g. with 1 value bit and 31 padding
> bits.
>