From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=BAYES_00,THIS_AD autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1cf653444208df72 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-10-21 19:08:05 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!jfk3-feed1.news.digex.net!dca6-feed2.news.digex.net!intermedia!newsfeed1.cidera.com!Cidera!cyclone1.gnilink.net!wn3feed!worldnet.att.net!135.173.83.71!wnfilter1!worldnet-localpost!bgtnsc06-news.ops.worldnet.att.net.POSTED!not-for-mail From: "David Thompson" Newsgroups: comp.lang.ada References: <9q7na102nqn@drn.newsguy.com> <87k7xwa93z.fsf@deneb.enyo.de> Subject: Re: ada vs. cpp X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: <9cLA7.135653$3d2.4077683@bgtnsc06-news.ops.worldnet.att.net> Date: Mon, 22 Oct 2001 02:08:05 GMT NNTP-Posting-Host: 12.89.145.89 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc06-news.ops.worldnet.att.net 1003716485 12.89.145.89 (Mon, 22 Oct 2001 02:08:05 GMT) NNTP-Posting-Date: Mon, 22 Oct 2001 02:08:05 GMT Organization: AT&T Worldnet Xref: archiver1.google.com comp.lang.ada:15006 Date: 2001-10-22T02:08:05+00:00 List-Id: Florian Weimer wrote : > lutz@iks-jena.de (Lutz Donnerhacke) writes: > > > OTOH one of my DSPs has Character'Size = 32, > > > > a pretty common problem to almost all C based programs, because they tend to > > use constructs like > > for (unsigned char c = 0; c < UCHAR_MAX + 1; c++) { ... } > > This code is never correct, "c < UCHAR_MAX + 1" is always true, I > think. > Yep. I think Lutz meant int /* or unsigned int */ c = 0; c<...; c++. Which he is right is the usual idiom. Most of the arithmetic operators in C promote their operands to at least (signed or unsigned) int before doing the operation. On most machines int is wider than unsigned char, so UCHAR_MAX + 1 can be computed as int without overflow or wraparound, and as long as the index variable is a type that can be incremented to that it terminates. But on a machine with 32-bit char and 32-bit (or smaller?!) int, this promotion no longer happens, so UCHAR_MAX + 1 is always zero, and the loop never executes. Another, arguably even clever/trickier, solution is to rely on the modular behavior of unsigned: unsigned char c = 0; do{ ... } while(++c /*!=0*/ ); (BTW- the syntax of declaring the loop variable in the for statement was not in C89; it is in C++ and C99. Workaround is obvious.) > > This DSP is always funny because sizeof(int[8]) == 4. > > Funny indeed. Is this really legal C? No. sizeof(char) is defined to be 1, and sizeof any other type must be at least 1; the common error is assuming strictly greater. sizeof(int[8]) = 8*sizeof(int) >= 8. The only C thing that can be smaller than char is a bit-field member of a struct, but that does not have its own type and cannot have its sizeof (or offsetof or address) taken. Even bool/_Bool occupies one "char" of space even though its value fits in one bit, and arrays of them are not packed. (But C++ std::vector _may_ be.) -- - David.Thompson 1 now at worldnet.att.net