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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1d321b3a6b8bcab2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-28 13:28:38 PST Path: swrinde!howland.reston.ans.net!lamarck.sura.net!darwin.sura.net!gwu.edu!seas.gwu.edu!dobrien From: dobrien@seas.gwu.edu (David O'Brien) Newsgroups: comp.lang.ada Subject: Re: "Subtract C, add Ada" Date: 28 Jan 1995 21:08:10 GMT Organization: George Washington University Message-ID: <3gebnq$7f3@cronkite.seas.gwu.edu> References: <3fo2ot$su2@miranda.gmrc.gecm.com> <1995Jan23.154631.6702@sei.cmu.edu> <3g4p94$g07@cronkite.seas.gwu.edu> <3g9rf0$71k@Starbase.NeoSoft.COM> NNTP-Posting-Host: 128.164.9.3 X-Newsreader: TIN [version 1.2 PL2] Date: 1995-01-28T21:08:10+00:00 List-Id: Samuel Mize (smize@Starbase.NeoSoft.COM) wrote: : In article <3g4p94$g07@cronkite.seas.gwu.edu>, : David O'Brien wrote: : >Robert Firth (firth@sei.cmu.edu) wrote: : > : >: LOOP : >: get(c); : >: EXIT WHEN c /= ASCII.SP; : >: END LOOP; : > : >: Note also that if there are *no more* non space characters, the C code : >: dies in an infinite loop, while the Ada code automatically does the : >: right thing, namely raises the END_ERROR exception. : > : If this is scanning a string that has no more spaces, Ada will : raise an exception at the end of the string. C will scan merrily : off the end of the array and through the memory following until it : hits a space or you get "segmentation error (core dumped)". Boy are we making assumptions about the processing that follows counting the number of spaces. Would you care to explain how C will core dump? The loop will stop when there are no more spaces IN A ROW. The original C code was: while ( (c = getchar()) == ' ' ) { /* count spaces */ } The Ada code and the C code ARE THE SAME. That being, both count the number of spaces in a sequence. Once the first non-space character is read (could be A thru Z, EOF, ASCII NUL, what ever), both of these code fragments will exit the while loop. There is nothing in these code fragments that may be used to complain about C's handling of strings. Code layout and where expressions may be used is what this thread was about. If you really want to get into it, the Ada way is worse. If the input string contained only spaces, Ada would exception when EOF was hit. C on the other hand would exit the loop cleanly and what ever came next would execute. In Ada I hope you wrapped the while loop with an exception handler. -- David O'Brien (dobrien@seas.gwu.edu)