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-31 10:07:22 PST Path: nntp.gmd.de!newsserver.jvnc.net!howland.reston.ans.net!swrinde!news.uh.edu!uuneo.neosoft.com!Starbase.NeoSoft.COM!not-for-mail From: smize@Starbase.NeoSoft.COM (Samuel Mize) Newsgroups: comp.lang.ada Subject: Re: "Subtract C, add Ada" Date: 31 Jan 1995 12:07:22 -0600 Organization: NeoSoft Internet Services +1 713 684 5969 Message-ID: <3glu8q$rv9@Starbase.NeoSoft.COM> References: <3fo2ot$su2@miranda.gmrc.gecm.com> <3g4p94$g07@cronkite.seas.gwu.edu> <3g9rf0$71k@Starbase.NeoSoft.COM> <3gebnq$7f3@cronkite.seas.gwu.edu> NNTP-Posting-Host: starbase.neosoft.com Date: 1995-01-31T12:07:22-06:00 List-Id: Some of my earlier postings were not as clear as they should have been. Let me clarify. In article <3gebnq$7f3@cronkite.seas.gwu.edu>, David O'Brien wrote: >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. >: > [discussion in Mize post of reading from a file deleted] >: 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? I meant, if the NUL at the end of a string was clobbered, or the array pointer was corrupted, and you started scanning the wrong part of memory. That isn't what I said, but is what I meant. I didn't clearly state that I was changing subject somewhat, and I also got the logic of the test backward. Sorry. If the end-marker of a string is missing, or the array pointer is corrupted, you can core dump right here. No further processing needed. > >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 They aren't the same code lexically or logically. They ARE not THE SAME. Someone else said that the C would go into an infinite loop, and that this was the equivalent Ada code. They were wrong. The logically equivalent Ada (using text_io) would be while not end_of_file loop get (c); exit when c /= ' '; end loop; >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. No, you just have to understand how EOF is handled in both languages, and write the Ada correctly. This was not your error originally, and since you're taking the pro-C side I won't dis you for not recognizing an Ada error. :-) The C code is more terse, but not necessarily more efficient; that depends on your compiler. The C getc is probably checking for EOF behind your back, instead of it being explicitly coded in. I am now switching topics back to strings. Someone in the thread brought up C going into an infinite loop. I thought that person may be confusing a file read with a string scan. (Yes, it *is* a file read; standard input is a file, as are standard output and standard error.) Anyway, on a string, you would ward the loop with a range check, probably using a "for" clause: for I in String'range loop exit when String(i) = ' '; end loop; or, if not starting at the start of the string: for I in Current_Pos .. String'last loop if String (I) = ' ' then Current_Pos := I; exit; end if; end loop; In C, with a string, most programmers would just look for a non-blank, including the NUL that is "always" at the end of a string. You can also explicitly code in a limit, as shown by a previous poster in this thread (I don't have it handy). In Ada, if you screw up and don't check the range, you can get an exception. In C, if you don't check the range, most people don't consider it an error, but if the data gets screwed up (no NUL or bad pointer) you can core dump. To bring this back to code layout: Ada provides a way to check the range as part of the loop construct, and a lexically-scoped way to handle the error case. In C, you *can* check for a bad array index but usually don't, and have no way to check whether the array pointer has been rewritten. You have to register a signal handler to deal gracefully with error cases. > >-- David O'Brien (dobrien@seas.gwu.edu)