redim - redimension array


redim [shared] Var(NumExpr[, NumExpr]. . .)
[, Var(NumExpr[, NumExpr]. . .)]. . . 


Redim erases and redimensions dynamic arrays.  When the program is being compiled, the redim statement declares arrays just like the dim statement except that the arrays are dynamic.  See dim for a description of the compile time behavior of this statement noting that arrays are always declared as dynamic.  When the program is run, redim acts like an erase and dim statement for each array.  See erase and dim for a description of the run time behavior of this statement. 


The following program fragment:
	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
illustrates 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. 


erase, redim


If the value of a subscript is greater than the maximum allowed or less than the minimum allowed, a "Subscript out of range" error occurs. 


Subscript range checking can be disabled by compiling with the "-C" option. 

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber