Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!sun-barr!apple!bbn.com!nic!news.cs.brandeis.edu!news!phils From: phils@chaos.cs.brandeis.edu (Phil Shapiro) Newsgroups: comp.sys.mac.programmer Subject: Re: Variable length arrays in Pascal--can it be done? Message-ID: Date: 11 Apr 91 17:38:04 GMT References: <1521@babcock.cerc.wvu.wvnet.edu> <1991Apr4.124257.3095@desire.wright.edu> Sender: usenet@news.cs.brandeis.edu Organization: Symantec Corp. Lines: 37 In-Reply-To: jmatthews@desire.wright.edu's message of 4 Apr 91 17:42:57 GMT In article <1991Apr4.124257.3095@desire.wright.edu> jmatthews@desire.wright.edu writes: In article <1521@babcock.cerc.wvu.wvnet.edu>, vrm@blackwater.cerc.wvu.wvnet.edu (Vasile R. Montan) 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. > ... > 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. Your technique is sound. I think that's how TextEdit's lineStarts array and the List Manager's cellArray work. Notice that only the lower array bound matters: it determines whether the array starts with element 0 or 1. The upper bound is immaterial since you'll never create an instance variable of this type, only a pointer to it. Moreover, you can keep the count with the array Type MyArray = record count:Integer; elements: array[1..1] of MyArrayElemnt end; I don't know about MPW Pascal, but THINK Pascal make some assumptions about how an array is indexed depending on its upper bound (really, the range over which it is bound). If you want to use the SetHandleSize method to expand/shrink an array, make sure that you dimension your array as the largest possible array that you're planning to resize it to. If you don't use this method, your programs will surely crash. -phil