size = 1 cnt = 0 while 1 dim temp(size) for i = 1 to cnt: temp(i) = list(i): next i size = size + size redim list(size) for i = 1 to cnt: list(i) = temp(i): next i erase temp while cnt < size input #1, list(cnt + 1) cnt = cnt + 1 wend wendillustrates the use of the redim statement. It reads a list of numbers from a file that was opened earlier. The array in which the numbers are stored grows dynamically as the numbers are read. When the input is exhausted a run time error will occur which will be trapped by an error recovery routine. At that point, the array "list" will contain the numbers, "size" will be the length of the array and "cnt" will be the number of numbers stored. "list" is repeatedly redimensioned to accommodate the growing list. When the redimensioning occurs, the previous contents of the array are lost, so the list is temporarily copied to another array "temp".
Copying and redimensioning the array for each number read would mean the algorithm would run in polynomial time. (The time it would take to read the nth number would be proportional to n.) Instead, the size of the array is doubled each time it is filled which means it runs in nearly linear time. (The time it takes to read the nth number is nearly independent of n). Still, on the average at least a quarter of the array will be wasted and if memory space is tight, in the worst case three times as much memory as needed must be available. But the time is usually worth the space.
from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber