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.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a5bde054ac3effc5 X-Google-Attributes: gid103376,public From: Marin David Condic Subject: Re: Unbounded string deallocation Date: 1999/08/26 Message-ID: <37C56E59.218CA190@pwfl.com>#1/1 X-Deja-AN: 517498476 Content-Transfer-Encoding: 7bit Sender: condicma@bogon.pwfl.com References: <37C46FD4.A42CC1A1@res.raytheon.com> <7q1tff$1n9$1@cnn.Princeton.EDU> <37C54509.17E067C1@res.raytheon.com> <37c552fd@news1.us.ibm.net> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Pratt & Whitney Mime-Version: 1.0 Reply-To: e108678@pwflcom Newsgroups: comp.lang.ada Date: 1999-08-26T00:00:00+00:00 List-Id: Matthew Heaney wrote: > > -- grab approx 1024 bytes of memory > > my_string := new ada.strings.unbounded_string.To_Unbound_String(1024); > > > > -- give back the 1024 bytes and grab 2048 new memory bytes > > my_string := new ada.strings.unbounded_string.To_Unbound_String(2048); > > > > end > > > > Is this correct? > > No, it is completely incorrect: > > o Do NOT use the allocator new. Any allocation is done implicitly, as part > of the call to To_Unbounded_String. > > o To_Unbounded_String takes type String as an argument; it does NOT take an > integer type. > In fairness, the package Ada.Strings.Unbounded does provide a To_Unbounded_String which will take a length as a parameter. It also provides an access type for the Fixed Strings. The example above is incorrect because it is mixing String_Access types with Unbounded_String types, but with a little adjustment, one could have done the dynamic allocation and used the Free procedure for deallocation - although I can't for the life of me think of why anyone would want to if nobody was standing over them with a stick forcing them to. IMHO, the To_Unbounded_String and To_String are just about all you need for most applications. And if Text_IO and some other basic things had Unbounded_String alternatives, you wouldn't even need these calls. I suppose there might be applications with efficiency issues, but for most things Unbounded_String is quite adequate. > > Here's what you do: > > declare > My_String : Unbounded_String; > begin > My_String := To_Unbounded_String ("this is a string"); > -- all allocation is implicit > > My_String := To_Unbounded_String ("this is another string"); > -- all deallocation is implicit > -- all allocation is implicit > > My_String := To_Unbounded_String ("this is yet another string"); > -- all deallocation is implicit > -- all allocation is implicit > end; > -- all deallocation is implicit A very clear example. I think that C programmers coming over to Ada have a tendency to want to grab pointers to everything or null-terminate strings unnecessarily just because that is what they are used to doing. Eventually, they get used to "The Ada Way". :-) MDC -- Marin David Condic Real Time & Embedded Systems, Propulsion Systems Analysis United Technologies, Pratt & Whitney, Large Military Engines M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600 ***To reply, remove "bogon" from the domain name.*** Visit my web page at: http://www.mcondic.com/