From owner-freebsd-arch@FreeBSD.ORG Mon Dec 5 18:16:32 2011 Return-Path: Delivered-To: freebsd-arch@FreeBSD.ORG Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8702E1065676; Mon, 5 Dec 2011 18:16:32 +0000 (UTC) (envelope-from das@FreeBSD.ORG) Received: from zim.MIT.EDU (ZIM.MIT.EDU [18.95.3.101]) by mx1.freebsd.org (Postfix) with ESMTP id 491398FC17; Mon, 5 Dec 2011 18:16:32 +0000 (UTC) Received: from zim.MIT.EDU (localhost [127.0.0.1]) by zim.MIT.EDU (8.14.5/8.14.2) with ESMTP id pB5IGVdl007595; Mon, 5 Dec 2011 13:16:31 -0500 (EST) (envelope-from das@FreeBSD.ORG) Received: (from das@localhost) by zim.MIT.EDU (8.14.5/8.14.2/Submit) id pB5IGV8V007594; Mon, 5 Dec 2011 13:16:31 -0500 (EST) (envelope-from das@FreeBSD.ORG) Date: Mon, 5 Dec 2011 13:16:31 -0500 From: David Schultz To: mdf@FreeBSD.ORG Message-ID: <20111205181631.GA7393@zim.MIT.EDU> Mail-Followup-To: mdf@freebsd.org, John Baldwin , freebsd-arch@freebsd.org, Zack Kirsch References: <20111130154604.B949@besplex.bde.org> <201111301032.04102.jhb@freebsd.org> <20111201014944.GA78010@zim.MIT.EDU> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Cc: Zack Kirsch , John Baldwin , freebsd-arch@FreeBSD.ORG Subject: Re: Use of bool / stdbool.h in kernel X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Dec 2011 18:16:32 -0000 On Sun, Dec 04, 2011, mdf@freebsd.org wrote: > On Wed, Nov 30, 2011 at 5:49 PM, David Schultz wrote: > > Unfortunately, the "new type" is mostly useless, aside from > > improving readability.  Unlike modern languages, C doesn't > > consider it a compile-time error to mix up bools and ints. > > Partly true. In addition to changing the semantics of the ++ and -- > operators, _Bool is usually implemented as an 8-bit type and so > pointers-to-bool and pointers-to-int are not compatible. Also, C (or > at least gcc? I don't know my C99 standard the way I know C89) will > apply default promotion and casting rules, so that e.g. an assignment > like: > > int a; > bool b; > > a = 2; > b = a; > printf("%d", b); > > will print 1. What I meant by "mostly useless" is that programs like the above don't elicit a compile-time error. Static checks to prevent the conflation of boolean and numeric types (unless you explicitly ask for it) would promote correctness. That's the most important benefit of a boolean type, but with C99 you don't get that. Kernighan even conceded as much in a paper advocating C over Pascal. The C99 bool type is still good for something because it documents what the programmer meant, but the semantics are bizarre. For instance, gcc allows the statement 'return 2' in a boolean function, and the result is that the function returns 1. And incidentally, the "modern languages" I mentioned that get it right include ALGOL 60!