Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!apple!mitch From: mitch@Apple.COM (Mitchell Adler) Newsgroups: comp.sys.mac.programmer Subject: Re: Variable length arrays in Pascal--can it be done? Message-ID: <13102@goofy.Apple.COM> Date: 17 Apr 91 06:23:41 GMT References: <1521@babcock.cerc.wvu.wvnet.edu> Organization: Apple Computer Inc., Cupertino, CA Lines: 48 In article <1521@babcock.cerc.wvu.wvnet.edu> un020070@vaxa.wvnet.edu writes: >I want to have variable length arrays in Pascal. [Reason for having variable length arrays deleted] >Suppose I define a type in Pascal which is an arbitrarily large >array: say, one with 10000 elements in it, or some number larger than >I will ever need. Suppose I then define a pointer to point to >this type, and a handle to point to the pointer. Let's say I want >to start out with seven elements in the array, so I use NewHandle >and allocate just enough memory to hold those seven elements. I >store this number seven in a variable to keep track of how big >the array is. > >Now, as long as I am careful not to refer to any elements beyond the >end of the memory block, I should not have any problems, right? >And if I want to resize the array, I can use SetHandleSize, >making sure to keep track of how many elements are now in the array, >right? Is there any reason why this shouldn't work? Umm..not quite. Pascal is defined to Range Check at RUN time. That is, if you access outside of the declared range it's a runtime error. So as long as your dynamic array is smaller than your declared array, you'd be OK for the range checking (assuming you don't write outside the actual size of the array, as you said). But if you ever tried to write to a location beyond the end of your declared size, you'd get a runtime error. MPW Pascal (and I believe Think Pascal also), have the ability to turn off runtime range checking. You'll need to turn it off everywhere you index into your dynamic array. In MPW Pascal you use the directive: {$R-} to turn runtime range checking for sets, arrays and strings off. {$R+} to turn it back on. >--Kurisuto >un020070@vaxa.wvnet.edu Mitch -- Mitch Adler mitch@apple.com Development Systems Apple Computer, Inc. AppleLink: M.Adler Claimer: These are MY opinions, not Darin's, not Apple's! MINE!