Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1BcR01-000059-Jd for qemu-devel@nongnu.org; Mon, 21 Jun 2004 11:46:21 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1BcQzz-00004x-Rn for qemu-devel@nongnu.org; Mon, 21 Jun 2004 11:46:21 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BcQzz-00004u-Nt for qemu-devel@nongnu.org; Mon, 21 Jun 2004 11:46:19 -0400 Received: from [64.253.108.95] (helo=talon.kainx.org) by monty-python.gnu.org with esmtp (Exim 4.34) id 1BcQyR-00038i-0K for qemu-devel@nongnu.org; Mon, 21 Jun 2004 11:44:44 -0400 Date: Mon, 21 Jun 2004 11:44:40 -0400 From: Michael Jennings Subject: Re: OT: C Q/As, was Re: [Qemu-devel] security_20040618 Message-ID: <20040621154440.GQ4686@kainx.org> References: <40D6A164.2010109@petig-baender.de> <20040620192652.GA1927@sentinelchicken.org> <40D6A164.2010109@petig-baender.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <40D6A164.2010109@petig-baender.de> Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org On Monday, 21 June 2004, at 10:50:44 (+0200), Christof Petig wrote: > can you enlighten me? The only drawback I see is that with plain C > (no C++) typedef enum { ... } BOOL; would be more appropriate. > > I would propose > #ifndef __cplusplus > typedef enum { false, true } bool; > #endif > as the optimal solution for a problem I hardly have (since I usually > don't go back to coding in C) There are two problems with using enum's and/or #define's for TRUE/FALSE. They should not be used in boolean expressions except where the return value is of that same typedef (e.g., the function returns bool) or is generated from the same set of #define's. While false is always 0, true is not always 1. True is non-zero. > const char *itoa(int i) > { char x[20]; > snprintf(x,sizeof x,"%d",i); > return x; > } Forgot "static" before char x[20];, or to be more threadsafe, either malloc() or pass in a buffer and max size. > defensive programming would require that TRUE be also defined as > > #define TRUE 1 > > as many unsuspecting programmers will expect TRUE and FALSE to be handled in > the preprocessor phase eg: > > #if TRUE > ... > somecode(); > ... > #endif I disagree strongly. Anyone who writes "#if TRUE" or "#if FALSE" is just asking for trouble; (s)he needs to be taught a lesson. "#if 0" and "#if 1" are just as obvious, and both "#if 1" and "#if TRUE" are equally ridiculous. A better technique would be something more like this: #define UNUSED_CODE_BLOCK 0 #if UNUSED_CODE_BLOCK ... #else /* used code below */ ... #endif But even then it isn't as clean and readable as 1 or 0. Plus, you've still failed to solve the problem that true may or may not be 1. Michael -- Michael Jennings (a.k.a. KainX) http://www.kainx.org/ n + 1, Inc., http://www.nplus1.net/ Author, Eterm (www.eterm.org) ----------------------------------------------------------------------- "You can accomplish much if you don't care who gets the credit." -- Ronald Reagan