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,26639dcc06d45917 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-20 12:08:13 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!ihug.co.nz!west.cox.net!cox.net!newsfeed1.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3DB2FF06.8060308@acm.org> From: Jeffrey Carter User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Array of unbounded strings, memory leakage? References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Sun, 20 Oct 2002 19:08:17 GMT NNTP-Posting-Host: 63.184.9.84 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.prod.itd.earthlink.net 1035140897 63.184.9.84 (Sun, 20 Oct 2002 12:08:17 PDT) NNTP-Posting-Date: Sun, 20 Oct 2002 12:08:17 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:29965 Date: 2002-10-20T19:08:17+00:00 List-Id: Eric G. Miller wrote: > I know the following isn't terribly efficient, but does the Resize > procedure leak memory? I wrote a little package for reading/writing > common delimited text file formats (tab, space, comma, etc...) and > this is what I'm using to hold the split contents of each "record". > If I understand correctly, it shouldn't leak, since I never "new" the > individual unbounded strings. > > Say I have: > > type UBString_Array is array (Positive range <>) of > Ada.Strings.Unbounded.Unbounded_String; > type UBStrArray_Access is access UBString_Array; > > ... > > procedure Free is new Ada.Unchecked_Deallocation ( > UBString_Array, UBStrArray_Access); > > procedure Resize (Fields : in out UBStrArray_Access; > Size : Natural) > is > Tmp : UBStrArray_Access := null; > Old_Size : Natural := 0; > begin > if Fields /= null then > Old_Size := Fields'Length; > end if; > if Size > 0 then > Tmp := new UBString_Array(1..Size); > if Size <= Old_Size then > Tmp(1..Size) := Fields(1..Size); > else > Tmp(1..Old_Size) := Fields(1..Old_Size); > for J in Old_Size + 1 .. Size loop > Tmp(J) := Ada.Strings.Unbounded.Null_Unbounded_String; > end loop; > end if; > end if; > Free (Fields); > Fields := Tmp; > end Resize; > This shouldn't leak memory. If it does, there's a problem with your compiler's implementation of Unbounded_String. Note that the loop to initialize the new elements could use an aggregate: Tmp (Old_Size + 1 .. Size) := (others => Null_Unbounded_String); However, since an uninitialized Unbounded_String has the default value of Null_Unbounded_String, this isn't necessary. -- Jeff Carter "Sons of a silly person." Monty Python & the Holy Grail