Newsgroups: comp.sys.apple2.programmer Path: news.uiowa.edu!news.physics.uiowa.edu!math.ohio-state.edu!howland.reston.ans.net!gatech!taco.cc.ncsu.edu!news-server.ncren.net!hearst.acc.Virginia.EDU!murdoch!watt.seas.Virginia.EDU!wrh3c From: wrh3c@watt.seas.Virginia.EDU (William R. Hall) Subject: Re: Dynamic Pascal arrays possible? X-Nntp-Posting-Host: watt.seas.virginia.edu Message-ID: Sender: usenet@murdoch.acc.Virginia.EDU Organization: University of Virginia References: <4cnkhe$9t9@news1.pld.com> Date: Wed, 10 Jan 1996 18:35:13 GMT Lines: 50 I suppose it depends on what you consider to be messy, but making a dynamic integer array isn't all that hard. Here's a simple method for implementing one of arbitrary size (up to the largest available block of memory): (Disclaimer: these code snippets have not been tested!) To allocate the array, given the number of elements desired: SizeOfHandle := NumberOfElements * 2; { integer is two bytes } ArrayHndl := NewHandle( SizeOfHandle, UserID, NIL); { check parameters for NewHandle - I'm not sure if these are right } To reference an element in the array (assuming a zero-based element number): (assuming a declaration of var iPtr : integerPtr; ) HLock( ArrayHndl); if (ElementNumber < 0) or (ElementNumber >= NumberOfElements) then { element number is out of range - don't use it } else iPtr := pointer( ord4( ArrayHndl^) + ElementNumber * 2); If the element was in range, iPtr now points to the desired element. It can be set or read with: iPtr^ := IntegerValue; { write } IntegerValue := iPtr^; { read } When you're finished accessing elements in the array, it's a good idea to: HUnlock( ArrayHndl); so that the Memory Manager can move it around in memory, if needed. Finally, when you're all done with the array, don't forget to free it using DisposeHandle. As an advanced topic, note that NumberOfElements does not need to stay the same. With corresponding resizing of ArrayHndl, the array size can be changed dynamically while the program is running. Okay, so it's not as simple as built-in Pascal arrays, but it isn't really complicated either. It would be a good idea to write a unit to implement the dynamic array, then use that unit in your program. -- William R. Hall | wrh3c@Virginia.edu