Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!bloom-beacon!wesommer From: wesommer@athena.mit.edu (William Sommerfeld) Newsgroups: comp.lang.c Subject: Re: Why should "sizeof" be unsigned? Message-ID: <976@bloom-beacon.MIT.EDU> Date: Sun, 21-Jun-87 16:12:52 EDT Article-I.D.: bloom-be.976 Posted: Sun Jun 21 16:12:52 1987 Date-Received: Mon, 22-Jun-87 07:02:15 EDT References: <1748@plus5.UUCP> <6001@brl-smoke.ARPA> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: wesommer@athena.mit.edu Distribution: world Organization: Massachusetts Institute of Technology Lines: 32 Keywords: unsigned ANSI Summary: unsigned sizeof can screw you in obscure ways. In article <6001@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: >In article <1748@plus5.UUCP> hokey@plus5.UUCP (Hokey) writes: >>None of these issues are key - it seems to me having sizeof return an >>explicitly unsigned value violates "the principle of least astonishment". > >(a) AT&T "sizeof" has been this way for many years now. But Berkeley's hasn't. >(b) Since sizeof(thing) inherently cannot be negative, an unsigned >integer value for the sizeof operator seems exactly right. This can run into problems in existing code.. For example, a couple of months ago, I spent several hours helping someone track down what appeared to be a DBM bug on the IBM RT/PC under IBM's BSD port. I finally tracked it down to the following statement: if (i1 <= (i2+3) * sizeof(short)) return (0); (in additem(), in lib/libc/gen/ndbm.c). By examining the assembler output by the compiler, I determined that it was probably generating an unsigned compare for that. (I say "probably" because I can't read RT assembler very well), and the behavior of the program was as if an unsigned test was being generated there. It turns out that the compiler was an ANSI C compiler. I mistakenly reported it as a compiler bug, and was corrected on it by someone who had actually done work on an ANSI C interpreter. Bill Sommerfeld wesommer@athena.mit.edu