Newsgroups: comp.sys.apple2.programmer Path: news.uiowa.edu!news.physics.uiowa.edu!newsrelay.iastate.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!newsfeed.internetmci.com!in2.uu.net!brighton.openmarket.com!decwrl!waikato!comp.vuw.ac.nz!actrix.gen.nz!dempson From: dempson@atlantis.actrix.gen.nz (David Empson) Subject: Re: Dynamic Pascal arrays possible? Message-ID: Sender: news@actrix.gen.nz (News Administrator) Organization: Actrix - Internet Services Date: Mon, 8 Jan 1996 21:03:50 GMT References: <4cnkhe$9t9@news1.pld.com> X-Nntp-Posting-Host: atlantis.actrix.gen.nz Lines: 60 In article <4cnkhe$9t9@news1.pld.com>, wrote: > I've got a Pascal program (a maze generator) that uses large static > arrays. Evidently, ORCA/Pascal includes space in the code file for those > arrays, so that when I increase the size of the arrays, the file really > starts to bloat (the actual source is pretty small... it's just a text > based file). Brilliant. I would have expected the compiler to output some kind of repeat construct (not sure if this exists in OMF), or a "DS" (skip bytes). It probably cannot do this is the array is initialized, but last time I looked, ORCA/Pascal doesn't have any method for initializing variables as they are defined. > I used to have the formula for indexing a one dimensional array (though > if I thought it out, I could probably come up with again) that I used to > use with Integer Basic, and I've been thinking that perhaps I could > allocate the memory w/NewHandle, then access each element with some > pointer math. Whoa! This is Pascal we are talking about here. It is much easier: program demo(output); type bigarray = array[1..1000] of integer; bigarrayptr = ^bigarray; var mybigarray: bigarrayptr; i: integer; begin new(mybigarray); for i := 1 to 1000 do mybigarray^[i] := 1001 - i; for i := 1000 downto 1 do write(mybigarray^[i], ' '); writeln; end. The only changes you need to implement a dynamic array are to declare a pointer type, a type for the array itself (these steps can probably be skipped, but it makes things easier), allocating the array with "new", and all accesses require the pointer lookup character (caret) between the array name and the subscript brackets. It gets rather messy if you want a variable sized dynamic array. Your best option then would be to do either of the following: 1. Allocate the array at the biggest size you will support, or 2. Allocate the memory with NewHandle (locked, fixed, less than 64K, don't cross a bank boundary), dereference the handle to get its address, and assign it to a pointer of the above general type (you must use a typecast). Then just make sure you don't run over the actual end of the memory area. -- David Empson dempson@actrix.gen.nz Snail mail: P.O. Box 27-103, Wellington, New Zealand