C0361@univscvm.csd.scarolina.edu ( Thomas Jenkins) (07/11/90)
Source for implementing Dynamic arrays in TP 5.5. I saw a request for how to do this and after reading (and much playing) this is what I ended up with. I've since used it to allocate arrays using: numElements := (MaxAvail DIV SizeOF(dataType); GetMem(dArray , ( SizeOf(DataType) * numElements) ); to grab the largest working area from the Heap. It works very well for me, hope it helps you too. See bottom of posting for addresses to send comments/flames/etc to tom (*$R-*) (*************************************************************************** The original request was from: From: Brynjolfur Thorsson <binni@rhi.hi.is> Subject: Dynamic arrays in TP5.0 ****************************************************************************) PROGRAM Dynamic; CONST MAX_NUMBER_OF_ELEMENTS = 10; TYPE DataType = REAL; DataArray = ARRAY (.1..1.) OF DataType; DynamicArrayType = ^DataArray; SubscriptType = WORD; VAR dArray :DynamicArrayType; data,num :DataType; x :SubscriptType; PROCEDURE PutVal(VAR dArray :DynamicArrayType; subscript :SubscriptType; value :DataType); BEGIN (*************** *** PutVal *** ***************) IF ( (subscript <= MAX_NUMBER_OF_ELEMENTS) AND (subscript > 0) ) THEN dArray^(.subscript.) := value ELSE BEGIN WriteLn('Error!! subscript (.',subscript:4,'.) out of range!!'); Halt(12); END;(*IF*) END;(**** PutVal ****) FUNCTION GetVal(VAR dArray :DynamicArrayType; subscript :SubscriptType) :DataType; BEGIN (*************** *** GetVal *** ***************) IF ( (subscript <= MAX_NUMBER_OF_ELEMENTS) AND (subscript > 0) ) THEN GetVal := dArray^(.subscript.) ELSE BEGIN WriteLn('Error!! subscript (.',subscript:4,'.) out of range!!'); Halt(12); END;(*IF*) END;(**** GetVal ****) BEGIN WriteLn('Memory before allocation:',MemAvail); GetMem( dArray, ( MAX_NUMBER_OF_ELEMENTS * SizeOf( DataType ) ) ); WriteLn('Memory after allocation:',MemAvail); WriteLn; FOR x := 1 TO MAX_NUMBER_OF_ELEMENTS DO PutVal( dArray , x , random(1234) ); FOR x := 1 TO MAX_NUMBER_OF_ELEMENTS DO WriteLn('Array element #',x:2,' contains ',GetVal(dArray,x):5:2); FreeMem( dArray, ( MAX_NUMBER_OF_ELEMENTS * SizeOf( DataType ) ) ); WriteLn; WriteLn('Memory after deallocation:',MemAvail); END. ****************************************************************************** Hope you fine this program useful: tom THOMAS E. JENKINS, JR. PROGRAMMER, UNIVERSITY OF SOUTH CAROLINA BITNET, C0361 AT UNIVSCVM.BITNET INTERNET C0361 AT UNIVSCVM.CSD.SCAROLINA.EDU