From: "Nick Roberts" <nickroberts@blueyonder.co.uk>
Subject: Re: Elegant 'realloc'?
Date: Thu, 31 Jul 2003 17:35:07 +0100
Date: 2003-07-31T17:35:07+01:00 [thread overview]
Message-ID: <bgbgbp$n6bbp$1@ID-25716.news.uni-berlin.de> (raw)
In-Reply-To: slrnbihq0e.2j7.lutz@taranis.iks-jena.de
"Lutz Donnerhacke" <lutz@iks-jena.de> 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]
next prev parent reply other threads:[~2003-07-31 16:35 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-07-31 9:57 Elegant 'realloc'? Lutz Donnerhacke
2003-07-31 14:42 ` Matthew Heaney
2003-07-31 14:59 ` Lutz Donnerhacke
2003-07-31 16:50 ` Matthew Heaney
2003-07-31 16:35 ` Nick Roberts [this message]
2003-08-01 0:01 ` Robert I. Eachus
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox