Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!bionet!agate!ucbvax!tut.cis.ohio-state.edu!uoft02.utoledo.edu!desire.wright.edu!jmatthews From: jmatthews@desire.wright.edu Newsgroups: comp.sys.mac.programmer Subject: Re: Variable length arrays in Pascal--can it be done? Message-ID: <1991Apr4.124257.3095@desire.wright.edu> Date: 4 Apr 91 17:42:57 GMT References: <1521@babcock.cerc.wvu.wvnet.edu> Organization: University Computing Services, Wright State University Lines: 47 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. > > 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? > > --Kurisuto > un020070@vaxa.wvnet.edu 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; Finally, I recently "discovered" the ToolBox Utility PtrAndHand. It's great in this context since it does the GetHandleSize, SetHandleSize and BlockMove stuff you'ld have to do yourself. You can always get a Ptr to the source data with the @ operator and the destination handle can be unlocked (PtrAndHand does the right thing internally, I think:-). Hope this helps, John o----------------------------------------------------------------------------o | John B. Matthews, jmatthews@desire.wright.edu, am103@cleveland.freenet.edu | | "Say...what's a mountain goat doing way up here in a cloud bank?" - Larson | o----------------------------------------------------------------------------o