qbasicquickbasic

Inconsistent REDIM behaviour in QuickBasic 4.5


I was writing a toy QBasic compiler and some tests for it. When I tried to create a vector type, I encountered an inconsistency with REDIM.

The following code works in both QBasic and QuickBasic 4.5 interpreters. However, it produces 'Subscript out of range' error for the second REDIM when compiled as EXE.

DECLARE SUB RedimIntArray (arr() AS INTEGER)
DECLARE SUB RedimLongArray (arr() AS LONG)

' $DYNAMIC
DIM xs(2) AS INTEGER
PRINT UBOUND(xs)
RedimIntArray xs()
PRINT UBOUND(xs)

' $DYNAMIC
DIM ys(2) AS LONG
PRINT UBOUND(ys)
RedimLongArray ys()
PRINT UBOUND(ys)

SUB RedimIntArray (arr() AS INTEGER)
    REDIM arr(10) AS INTEGER
END SUB

SUB RedimLongArray (arr() AS LONG)
    REDIM arr(10) AS LONG
END SUB

Is it something expected and documented somewhere, and if so, are there any possible fixes for this?

UPD: The program above works fine on QBX 7.1 and QB64, so it might be something to do with the QB 4.5 compiler.


Solution

  • It's indeed a bug in the compiler.

    This KB article describes the issue: Q50638: "Subscript Out Of Range" If REDIM Long Integer Array in SUB.

    REDIMing (redimensioning with REDIM) a dynamic long integer array that
    was passed to a SUBprogram generates a "Subscript Out Of Range" error
    at run time.
    
    Microsoft has confirmed this to be a problem in Microsoft QuickBASIC
    Versions 4.00, 4.00b, and 4.50 for MS-DOS and in Microsoft BASIC
    Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2 (buglist6.00,
    buglist6.00b). This problem was corrected in Microsoft BASIC PDS
    Version 7.00 (fixlist7.00).
    
    The "Subscript Out Of Range" occurs whether the SUBprogram is compiled
    as part of the main program or it is compiled in a separate module.
    
    You can work around this problem by using an array type other than
    long integer, or by passing the array through a COMMON SHARED block.