Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!midway!mimsy!mojo!russotto From: russotto@eng.umd.edu (Matthew T. Russotto) Newsgroups: comp.sys.mac.programmer Subject: Re: Variable length arrays in Pascal--can it be done? Message-ID: <1991Apr3.034030.27719@eng.umd.edu> Date: 3 Apr 91 03:40:30 GMT References: <1521@babcock.cerc.wvu.wvnet.edu> Sender: news@eng.umd.edu (C-News) Organization: College of Engineering, Maryversity of Uniland, College Park Lines: 31 In article <1521@babcock.cerc.wvu.wvnet.edu> un020070@vaxa.wvnet.edu writes: >I want to have variable length arrays in Pascal. Pascal, however, >does not want me to have variable length arrays in it. I think I've >figured out a way to do this. This solution is probably glaringly >obvious to more experienced programmers, but I want to run it >past you to see if there's any reason why it shouldn't work. > >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? I believe this is similiar to the way Apple does it for CTabHandles and other variable-length arrays. They declare theirs to be TYPE CSpecArray: Array [0..0] of ColorSpec; and turn off range checking so the references succeed. If you want range checking effective for other arrays, your method should work. -- Matthew T. Russotto russotto@eng.umd.edu russotto@wam.umd.edu .sig under construction, like the rest of this campus.