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,ddba2f4eebde1467 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-31 09:32:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!82-43-33-75.cable.ubr01.croy.blueyonder.co.UK!not-for-mail From: "Nick Roberts" Newsgroups: comp.lang.ada Subject: Re: Elegant 'realloc'? Date: Thu, 31 Jul 2003 17:35:07 +0100 Message-ID: References: NNTP-Posting-Host: 82-43-33-75.cable.ubr01.croy.blueyonder.co.uk (82.43.33.75) X-Trace: news.uni-berlin.de 1059669177 24325497 82.43.33.75 (16 [25716]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 Xref: archiver1.google.com comp.lang.ada:41083 Date: 2003-07-31T17:35:07+01:00 List-Id: "Lutz Donnerhacke" wrote in message news:slrnbihq0e.2j7.lutz@taranis.iks-jena.de... > When dealing with dynamically allocated variable length > arrays, the allocated space might change. Is there a > common idiom to simulate an 'realloc' (especially > shrinking) other than, allocate, copy, free? Nope. > declare > procedure Free is new Unchecked_Deallocation(T_Array, T_Array_Access); > oldp : constant T_Array_Access := current.field; > begin > current.field := new T_Array'(oldp(oldp'First .. current.last)); > Free(oldp); > end; The above isn't quite right. Supposing we have the declarations: type Location_Descriptor is ... Unknown_Location: constant Location_Descriptor; type Location_Array is array (Integer range <>) of Location_Descriptor; type Location_History is access Location_Array; Track: Location_History; procedure Free is new Unchecked_Deallocation( Location_Array, Location_History ); At a point where we wanted to change the range of Track to, let's say, New_First..New_Last, we could write: declare subtype NI is Nonpositive_Integer; -- convenient renaming Mid_First: constant NI := NI'Max( Track'First, New_First ); Mid_Last: constant NI := NI'Min( Track'Last, New_Last ); Temp: constant Location_History := new Location_Array( New_First .. New_Last ); begin Temp( Mid_First .. Mid_Last ) := Track( Mid_First .. Mid_Last ); Free( Track ); Temp( New_First .. Mid_First-1 ) := (others => Unknown_Location); Temp( Mid_Last+1 .. New_Last ) := (others => Unknown_Location); Track := Temp; end; I haven't tested this, but I think it's right. -- Nick Roberts Jabber: debater@charente.de [ICQ: 159718630]