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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1042f393323e22da X-Google-Attributes: gid103376,public From: Jeff Carter Subject: Re: Any research putting c above ada? Date: 1997/05/07 Message-ID: <33709599.167EB0E7@spam.innocon.com>#1/1 X-Deja-AN: 239984299 References: <5ih6i9$oct$1@waldorf.csc.calpoly.edu> <5ijb0o$ajc@ns1.sw-eng.falls-church.va.us> <334d3da5.14386594@aplcen.apl.jhu.edu> <2senchydgk.fsf@hpodid2.eurocontrol.fr> <5im3an$3dv@bcrkh13.bnr.ca> <2sybamvslk.fsf@hpodid2.eurocontrol.fr> <5ius80$1nr8@newssvr01-int.news.prodigy.com> <335ae79e.55ed@dynamite.com.au> <5jde9l$u8q@newssvr01-int.news.prodigy.com> <33643f1f.0@news2.maynick.com.au> <5k3fma$126a@newssvr01-int.news.prodigy.com> <336eda35.0@news2.maynick.com.au> Organization: Innovative Concepts, Inc. Newsgroups: comp.lang.ada Date: 1997-05-07T00:00:00+00:00 List-Id: Kevin Cline wrote: > > amd001@its.maynick.com.au (Andrew Dunstan) wrote: > > >The commonest errors (by far) that I have seen in > >years of debugging other people's code (and some of my own) have been > >pointer mismanagement and off-by-one errors in array processing. Ada > >really does shine in helping you avoid these (as well as having many > >other good points). > > How does Ada help you avoid the problem of managing dynamically allocated > memory? Well, let's suppose you have a text file with N+1 lines, where N is not known until run time. The 1st line is the string representation of an integer (>= 0), N. The remaining N lines contain strings of varying lengths. In C, for example, I think you would have to read the value of N, dynamically allocate an array of N pointers to char, then read each string and dynamically allocate space for it pointed to by the appropriate pointer in the array. You would also be responsible for correctly deallocating this space when you are finished with it. I don't have a great deal of C experience, so please pardon any errors I have made. The important point is that this must involve explicit allocation and deallocation. In Ada (even Ada 83), no explicit allocation and deallocation is required: function Get_Line (File : Text_Io.File_Type) return String; Text_Io.Create (File => File, ... declare Image : constant String := Get_Line (File); Num_Lines : constant Natural := Natural'Value (Image); Line : array (1 .. Num_Lines) of Variable_String; begin Read : for I in Line'range loop Line (I) := To_Variable_String (Get_Line (File) ): end loop Read; Text_Io.Close (File => File); -- use Line end; In Ada 83, you have to provide your own variable-length strings, which would have to be bounded to provide automatic deallocation. In Ada, you can use Ada.Strings.Bounded or Ada.Strings.Unbounded. -- Jeff Carter PGP:1024/440FBE21 Auntie-spam reply to; try ( carter @ innocon . com ) "Now go away, or I shall taunt you a second time." Monty Python & the Holy Grail