Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rochester!cornell!uw-beaver!mit-eddie!husc6!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Why should "sizeof" be unsigned? Message-ID: <586@haddock.UUCP> Date: Fri, 19-Jun-87 12:05:50 EDT Article-I.D.: haddock.586 Posted: Fri Jun 19 12:05:50 1987 Date-Received: Mon, 22-Jun-87 03:31:13 EDT References: <1748@plus5.UUCP> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Distribution: world Organization: Interactive Systems, Boston Lines: 26 In article <1748@plus5.UUCP> hokey@plus5.UUCP (Hokey) writes: >[In our application] there are 3 interesting cases: no string, a zero-length >string, and >0 length strings. We thought it would be a swell idea to denote >the "no string" case with a length counter of -1. [But we sometimes want to >make the test] "length > sizeof local_string_buffer" [which causes problems >because the special-case value of length is coerced to (unsigned)-1]. It >seems to me having sizeof return an explicitly unsigned value violates "the >principle of least astonishment". I think the crux of the problem is that your variable "length" is logically a union of "size_t" and a single out-of-band value which has nothing to do with sizes. It is not all that astonishing that you get incorrect results if you use an untested variable containing the OOB value as if it were a size_t. To answer the title question, size_t is unsigned because (a) it's always nonnegative, and (b) the corresponding signed datatype may not be wide enough. Having it be unsigned only on those machines where it's necessary would cause even more astonishment when trying to port code. >Then again, I think the size_t is pretty useless and that the proposed C >standard contains far too many typedefs that work against the good programmer. If size_t were not part of the standard, what type would you use for, say, the argument to malloc()? Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint