Path: utzoo!attcan!uunet!van-bc!ubc-cs!alberta!dvinci!news From: lowey@herald.usask.ca (Kevin Lowey) Newsgroups: comp.lang.pascal Subject: Re: Dynamic arrays in TP5.0 Message-ID: <1990Jul13.051356.10883@dvinci.usask.ca> Date: 13 Jul 90 05:13:56 GMT References: <1831@krafla.rhi.hi.is> Sender: news@dvinci.usask.ca Reply-To: lowey@herald.usask.ca Organization: University of Saskatchewan Lines: 63 From article <1831@krafla.rhi.hi.is>, by binni@rhi.hi.is (Brynjolfur Thorsson): > Hi there. > > I am trying to use dynamic arrays in my TP5.0 program but am having a hard > time. I want to be able to do things like they are done in C, e.g. The following code is VERY Turbo Pascal specific. I compiled it under TP 5.5, but it should work with 4.0 and higher. There are one main feature of Turbo Pascal 5.5 which facilitates dynamic arrays, the range checking option {$R-}. If you turn the range checking OFF, then you can index past the end of the array without generating a range check error. This lets you define an array [1..1] of whatever, and then go as high as you want (within the limits of the maximum index size). IMPORTANT: remember the RANGE CHECKING IS TURNED OFF, so you MUST make sure that you don't go past the end of the array, otherwise you can really screw things up. Here is the program. It defines the variables, reserves memory for an array of size 10, assigns values to the ten items in the array, writes these values back out again, then frees the memory up. program test; { Written by Kevin Lowey - Copy freely } Type Dummy_array = array [1..1] of real; { with range checking off, you can address as high as you want } VAR Array_Ptr : ^dummy_array; { For use with GETMEM to allocate array memory } i : integer; { for indexing through loops } begin { Allocate memory for array of size 10 reals } { If the SIZEOF(REAL) doesn't work in your TP, create a dummy } { REAL_VAR : REAL } variable and then use SIZEOF(REAL_VAR). You } { might have to do that in TP 4.0 and earlier } getmem (Array_Ptr,sizeof(Real) * 10); { Assign values to the array } for i := 1 to 10 do begin {$R-} {turn off range checking } array_ptr^[i] := 100.0+i; {$R+} {turn range checking on again } end; { Show that it worked } for i := 1 to 10 do begin {$R-} writeln (i:3,' ',array_ptr^[i]:3:0); {$R+} end; { Free up the memory when we are done } freemem (Array_Ptr,sizeof(real) * 10); end. - Kevin Lowey