"Subscript out of Range" Long-Integer Array in IF/THEN in 4.50 (57926)






This article was previously published under Q57926

SYMPTOMS

The QuickBasic Version 4.50 program shown below produces a "Subscript out of range" error if compiled with the BC /D (debug) switch. This appears to be caused by a compiler optimization problem for the particular combination of using a long-integer variable to subscript a long-integer array in an IF ... THEN statement.

If the program is run from the QB.EXE environment or is not compiled with the BC /D switch, the error does not occur.

STATUS

Microsoft has confirmed this to be a bug in Microsoft QuickBasic Version 4.50. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

This problem does not occur in versions of QuickBasic prior to Version 4.50, in Microsoft Basic Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2, or in Microsoft Basic Professional Development System (PDS) Version 7.00 for MS-DOS and MS OS/2.

MORE INFORMATION

The following program exhibits the behavior:
   DIM a(10) AS LONG
   a(1) = 5
   k& = 5
   i& = 1
   IF a(i&) = k&  THEN
      a(i&) = -k&       'this statement causes the error to occur
   END IF
				
Compile and link the program as follows:
   BC /D TEST;
   LINK TEST;
				
The "Subscript out of range" error does not occur if the IF ... THEN syntax is removed or if the variables i and k are of any type other than long integers.

To work around the problem, replace the problem-assignment statement, a(i&) = -k&, with either of the following equivalent assignments:

  1. a(i&) = k&
    a(i&) = -a(i&)
  2. a(i&) = -a(i&)

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB57926