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 07:42:29 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: mheaney@on2.com (Matthew Heaney) Newsgroups: comp.lang.ada Subject: Re: Elegant 'realloc'? Date: 31 Jul 2003 07:42:27 -0700 Organization: http://groups.google.com/ Message-ID: <1ec946d1.0307310642.74d93921@posting.google.com> References: NNTP-Posting-Host: 66.162.65.162 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1059662548 20345 127.0.0.1 (31 Jul 2003 14:42:28 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 31 Jul 2003 14:42:28 GMT Xref: archiver1.google.com comp.lang.ada:41078 Date: 2003-07-31T14:42:28+00:00 List-Id: Lutz Donnerhacke wrote in message news:... > 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? > > 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; I don't know how to implement a realloc in Ada, unless you write your own storage pool. You can try using the vector container in Charles, which hides all the allocate, copy, and free calls. The length of the vector refers to the number of active elements in the vector. The size of the vector refers to the length of the underlying array, which is at least the number of elements. To increase the size of a vector, you can just call Resize. To decrease the size of a vector, you use the swap trick: procedure Decrease_Size (V : in out Vector_Subtype) is V2 : Vector_Subtype := V; begin Swap (V, V2); end; Charles makes a guarantee that the new vector V2 has the minimum size necessary to contain all the elements in source vector V. (Presumably the size of V is much larger than its length, which is why we want to shrink it.) When you swap, the new vector V2 gets the (large) internal array of V, and V gets the (small) internal array of V2. V2 is immediately finalized, which free's the internal array (and which originally belonged to V). When you Clear a vector, like this: Clear (V); all this does is set the count of the number of active elements (the length) to 0. It leaves the internal array untouched, which means the size doesn't change. If you want to remove all the elements in a vector, and set the size of the vector to 0, then you can use the swap trick: procedure Clear_And_Decrease_Size (V : in out Vector_Subtype) is V2 : Vector_Subtype; --Length and Size are both 0 begin Swap (V, V2); end; Charles makes a guarantee that there is no cost for simply declaring a vector, in the sense that no internal memory is allocated during elaboration of the vector object. The Charles library is available from my home page. http://home.earthlink.net/~matthewjheaney/charles/ Look for a new release of Charles early next week.